Node Rest Client Response Error

2019-08-18 00:58发布

问题:

Can I handle a non-JSON response in node-rest-client POST method?

This is the error and response i'm getting:

response: [PURGED], error: [SyntaxError: Unexpected token P in JSON at position 0]

Can I somehow avoid getting an error? The function does what is requested. I suppose creating a custom parser is a solution? I don't understand how to achieve it though.

回答1:

You need to add a "mimetypes" attribute to your client options. For example, something like this should enable you to handle XML as well as JSON responses:

const Client = require('node-rest-client').Client;
var client = new Client({mimetypes:{
    json:["application/json","application/json;charset=utf-8"],
    xml:["application/xml","application/xml;charset=utf-8"]
}});
client.post(...)