Javascript 节点JS请求承诺POST方法不';我认不出字符串
为了将node.js模块与另一台服务器连接,我使用了“request-promise”库。当我想发布如下内容时:Javascript 节点JS请求承诺POST方法不';我认不出字符串,javascript,node.js,api,Javascript,Node.js,Api,为了将node.js模块与另一台服务器连接,我使用了“request-promise”库。当我想发布如下内容时: rp.({ method: 'POST', headers:{ 'Content-Type': 'text/plain', 'Authorization': '' }, uri: 'https://myendpoint.com', body:{ name: 'here is my a name', code: 'here i
rp.({
method: 'POST',
headers:{
'Content-Type': 'text/plain',
'Authorization': ''
},
uri: 'https://myendpoint.com',
body:{
name: 'here is my a name',
code: 'here is my code',
location: 'here is my location'
}
}).then(...)
RequestError: Error: Argument, error, options.body.
TypeError: First argument must be a string or Buffer
但当我尝试使用此函数时,总是会收到如下错误消息:
rp.({
method: 'POST',
headers:{
'Content-Type': 'text/plain',
'Authorization': ''
},
uri: 'https://myendpoint.com',
body:{
name: 'here is my a name',
code: 'here is my code',
location: 'here is my location'
}
}).then(...)
RequestError: Error: Argument, error, options.body.
TypeError: First argument must be a string or Buffer
正文中的所有参数都是字符串。我也在邮递员身上尝试了同样的方法,这对我很有效。那么我做错了什么
将数据发布到JSON REST API
将option.body
设置为您的数据,并将json:true
编码为
JSON
from:或者,您可以先对对象进行JSON字符串化,然后将其传递给body参数,而无需将JSON参数设置为true:
const myObject = {
name: 'here is my a name',
code: 'here is my code',
location: 'here is my location'
};
const stringifiedObject = JSON.stringify(myObject);
rp.({
method: 'POST',
headers:{
'Content-Type': 'text/plain',
'Authorization': ''
},
uri: 'https://myendpoint.com',
body: stringifiedObject
}).then(...)
更改
内容类型
:应用程序/json