0
votes

I am using Mulesoft Anypoint Platform to design a REST API. I am expecting a JSON response from REST API but I am getting XML response.

I have written below RAML file -

#%RAML 1.0
baseUri: https://mocksvc.mulesoft.com/mocks/aa0b3fb6-9fd7-4196-991e-d030a25bc84b # 
title: American Flights API
version: 1.0
mediaType: application/json

/flights:
  post:
    body:
      application/json:
        example:
          {
            "code": "GQ574",
            "price": 399,
            "departureDate": "2016/12/20",
            "origin": "ORD",
            "destination": "SFO",
            "emptySeats": 200,
            "plane": {"type": "Boeing 747", "totalSeats": 400}
          }
    responses: 
      201:
        body:
          application/json:
            example: | 
              {"Message": "Flight added (but not really)"}

I am getting response as below -

<response>
<Message>Flight added (but not really)
</Message>
</response>

In my RAML response body I have mentioned application/json. So not sure why it is not showing JSON response I have written in example and also why it is returning "application/xml" in response code when I have nowhere mentioned "application/xml".

Response headers has Content-Type "application/xml"

content-type:
application/xml; charset=utf-8
date:
Tue, 20 Feb 2018 15:54:26 GMT
server:
nginx
vary:
X-HTTP-Method-Override, Accept-Encoding
x-newrelic-app-data:
PxQFUVNQCwQTUVhXDwcDUFITGhE1AwE2QgNWEVlbQFtcCxYkSRFBBxdFXRJJM3dgZEtOPGttGAsLUl1APjpMSh5IB0sQZGgdHU8QRR5DH1JIBhlRV1MLBQlXVlcbEwBQRh0UVVEHAAYBAVkECg8FCgNHFQdQDUAHOQ==
x-powered-by:
Express
x-request-id:
7a6ebcf4-cc45-491c-8622-00fad8ef3a3e
content-length:
76
connection:
keep-alive

I want to know how I can change the Content-Type to "application/json" in response.

Please help.

3

3 Answers

0
votes

The specified content-type in the body should be application/json instead of application/xml

responses: 
      201:
        body:
          application/json:
            example: | 
              {"Message": "Flight added (but not really)"}
0
votes

On your code to set content-type, you can try adding HTTP Response Builder on your HTTP Component

<http:listener config-ref="HTTP_Listener_Configuration" path="/sample" allowedMethods="get" doc:name="HTTP" >
     <http:response-builder>
         <http:header headerName="Content-Type" value="application/json"/>
     </http:response-builder>
</http:listener>

Or try using Property Component at the end of your flow

<set-property propertyName="Content-Type" value="application/json" doc:name="Property"/>
0
votes

I was using Mozilla firefox earlier in which I was getting the issue that service was returning response in "application/xml" instead of "application/json" which I had written in RAML.

Now I login in anypoint platform in Chrome and in chrome it is working fine. Mock service is returning the response what provided in example and in JSON format.

Still not sure why it is behaving differently in different browser.