Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 节点JS请求承诺POST方法不';我认不出字符串_Javascript_Node.js_Api - Fatal编程技术网

Javascript 节点JS请求承诺POST方法不';我认不出字符串

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

为了将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 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