Anypoint Platform RAML模拟服务中的响应标头“Content-Type”不正确

问题描述 投票:0回答:3

我正在使用Mulesoft Anypoint Platform来设计REST API。我期待REST API的JSON响应,但我得到了XML响应。

我写了下面的RAML文件 -

#%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)"}

我得到的答复如下 -

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

在我的RAML响应主体中,我提到了application / json。所以不确定为什么它没有显示我在例子中写的JSON响应以及为什么它在响应代码中返回“application / xml”时我没有提到“application / xml”。

响应标头具有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

我想知道如何将Content-Type更改为“application / json”作为响应。

请帮忙。

mocking mule content-type raml
3个回答
0
投票

正文中指定的内容类型应该是application / json而不是application / xml

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

0
投票

在设置内容类型的代码上,您可以尝试在HTTP组件上添加HTTP Response Builder

<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>

或者尝试在流程结束时使用Property Component

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

0
投票

我之前使用的是Mozilla firefox,我遇到的问题是服务在“application / xml”中返回响应而不是我在RAML中编写的“application / json”。

现在我登录Chrome和Chrome中的任何一个平台,它运行正常。模拟服务返回示例和JSON格式中提供的响应。

仍然不确定为什么它在不同的浏览器中表现不同。

© www.soinside.com 2019 - 2024. All rights reserved.