节点休息客户端响应错误 [英] Node Rest Client Response Error

查看:66
本文介绍了节点休息客户端响应错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在 node-rest-client POST 方法中处理非 JSON 响应吗?

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

这是我得到的错误和响应:

This is the error and response i'm getting:

响应:[PURGED],错误:[SyntaxError:JSON 中的意外标记 P位置 0]

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.

推荐答案

您需要将mimetypes"属性添加到您的客户端选项.例如,这样的事情应该使您能够处理 XML 和 JSON 响应:

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(...)

这篇关于节点休息客户端响应错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆