JSON to XML for error case

Hi,
I have created an API by specifying HTTP service as a target. The source system will accept the only XML. Since I have added OAuth 2.0 feature in case of Authorization fail the response from Tyk gateway is in JSON format. How to resolve this issue.

Regards,
Mohamed Shalik

Hi Mohammed

Could you provide a bit more information about tglhe request flow and what you need to happen ideally?

Hello Josh,

Here I have attached the request with invalid Key and the response. I need this response in XML format.

Request

Content-Type: text/xml
Authorization: Bearer nb3557bfb528d49707bf1d8878a5e19a491447d8679267c193d0621

<soapenv:Envelope xmlns:soapenv=“http://schemas.xmlsoap.org/soap/envelope/”>
<soap:Header xmlns:soap=“http://schemas.xmlsoap.org/soap/envelope/”>
<oas:Security xmlns:oas=“http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd” >

		<oas:UsernameToken>
			<oas:Username>TestUser</oas:Username>
			<oas:Password>Welcome123</oas:Password>
			<!--You may enter ANY elements at this point-->
		</oas:UsernameToken>
	</oas:Security>
</soap:Header>
<soapenv:Body>
	<GetTrackandTraceRequest >
		<tracking_number>27562056007</tracking_number>
	</GetTrackandTraceRequest>
</soapenv:Body>

</soapenv:Envelope>

Response

content-type: application/json

{
“error”: “Key not authorised”
}

So you want the error response to be in XML? That is not possible without some kind of custom middleware which would hijack the request pre-auth and send back an XML response.

Sorry if i’m not understanding you well

Yes I need response to be XML pre-auth. Do you have any document or blog for the same. Can you elaborate the custom middleware and hijack request part

Hi

Ultimately Tyk is a REST Gateway so is not designed to return XML from it’s internals. However you could use something like javascript preauth middleware to check if Authorization header is present and return xml response if not https://www.tyk.io/docs/customise-tyk/plugins/javascript-middleware/middleware-scripting-guide/