简体   繁体   English

Node.js使用Json-Body发送Get-Request

[英]Node.js send Get-Request with Json-Body

I'm using Node.js and need to send a get-request with a Json-Message in the body. 我正在使用Node.js,需要在正文中发送带有Json-Message的get-request。

request = require('request-json');    
var client = request.createClient('http://ip');
client.get('/url', jsondata, function(err, res, body) {...

does not seem to send any Body-Data. 似乎没有发送任何身体数据。

Any ideas how to manage? 任何想法如何管理?

You can try : https://github.com/request/request . 您可以尝试: https : //github.com/request/request With that you do request.get or request.post . 这样,您就可以执行request.getrequest.post

This should work: 这应该工作:

var request = require('request'), 
    url = 'http://127.0.0.1', 
    jsondata = {data1: 'x', data2: 'y'};

request({url: url, qs: jsondata}, function(err, response, body) {
  if(err) { console.log(err); return; }
  console.log(response);
});

jsondata should be an object (your query string to send to the server, in a browser would be url/?data1=x&data2=y ) jsondata应该是一个对象(要发送到服务器的查询字符串,在浏览器中为url/?data1=x&data2=y

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM