我正在使用柴来获得回应。相反,我总是在文本中得到我想要的结果。我试过了:
.post('xyz.com/server')
.set('Cookie', 'somevalue=1234')
.set('content-type', 'application/json')
当我打印出我的回复对象时,我从未得到过身体。这就是我的回答:
"header": {
"x-powered-by": "Express",
"access-control-allow-origin": "*",
"access-control-allow-headers": "Origin, X-Requested-With, Content-Type, Accept",
"access-control-allow-credentials": "true",
"access-control-allow-methods": "Content-Type",
"content-type": "application/json; charset=utf-8",
"content-length": "107",
"etag": "W/\"6b-rgYrpZXb7TdJm/JOuUivlL3U1fA\"",
"vary": "Accept-Encoding",
"date": "Wed, 31 Jan 2018 19:38:24 GMT",
"connection": "close"
},
"status": 200,
"text": "{\"err\":\"1006\",\"errDesc\":\"Sorry, your request could not be completed at this time. Please try again later.\"}"
}
我需要这个文字进入正文。提前致谢
修改
临时解决方案是使用JSON.parse(res.text)将其转换为我需要从响应主体获取的JSON。但如果有人知道原始问题的解决方案,请发布。
答案 0 :(得分:0)
我使用了一个通用的Serializing对象,它接受一个动态对象并返回一个HTTPResponseMessage ..就像这个C#方法一样:
public HttpResponseMessage ToJson(dynamic obj)
{
var response = Request.CreateResponse(HttpStatusCode.OK);
response.Content = new StringContent(JsonConvert.SerializeObject(obj), Encoding.UTF8);
return response;
}