Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/vim/5.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
Node.js NodeJ使用请求发送头中的数据_Node.js_Header_Request - Fatal编程技术网

Node.js NodeJ使用请求发送头中的数据

Node.js NodeJ使用请求发送头中的数据,node.js,header,request,Node.js,Header,Request,服务器只接受头中发送的数据 使用此代码执行此操作时,服务器将获得空对象: const request = require('request') request.post({ url: 'https://.....', body: { userid: 'cris', gameid: '12' }, headers: { "Content-Type": "application/x-www-form-urlencoded"} }) 通过使用Postman,服务器将获得正确的

服务器只接受头中发送的数据

使用此代码执行此操作时,服务器将获得空对象:

const request = require('request')
request.post({
    url: 'https://.....',
    body: { userid: 'cris', gameid: '12' },
    headers: { "Content-Type": "application/x-www-form-urlencoded"}
})
通过使用Postman,服务器将获得正确的数据:

如何使用代码发送标题中的数据?


编辑:

浏览器中显示服务器信息的打印屏幕应该会有所帮助

试试这个

 const request = require('request')
   request.post({
   url: 'https://.....',
   body: JSON.stringify({ userid: 'cris', gameid: '12' }),
   headers: { "Content-Type": "application/x-www-form-urlencoded"}
})
试试这个

body: { "userid": "cris", "gameid": "12" }
试试这个

 const request = require('request')
 request.post({
   url: 'https://.....',
   headers: { 'Content-Type': 'application/json', 'Accept': 'application/json, text/plain', 'userid':'cris', 'gameid':'12'}
})

相同的空对象。我没想到要把它串起来。我想以某种方式发送headers属性,或者另一个我找不到的特定属性:-/我找到了一些关于服务器的浏览器信息,并将其附加到问题中。它说它接受文本,但stringify似乎没有帮助。它说的是错误,第一个参数必须是字符串或缓冲区。还尝试了
body:JSON.stringify({“userid”:“cris”,“gameid”:“12”}),
使用相同的空对象结果。我已经尝试过了,它正在工作
const request=require('request')request.post({url:'http://localhost:3000/test,标题:{“内容类型”:“应用程序/x-www-form-urlencoded”},正文:{userid:'cris',gameid:'12'}},function(err,res){console.log(res)}
如果我使用Postman
x-www-form-urlcoded
正文中发送相同的数据,服务器将得到相同的空对象。
未捕获类型错误:第一个参数必须是字符串或缓冲区
相同的东西:(.i hate Headers>如果不需要正文,请将其删除。完全删除正文,与您的示例一起使用!感谢Mustafa!