使用带requestjs的请求正文

时间:2018-06-10 18:06:27

标签: javascript node.js request requestjs

我正在使用requestjshttps://www.handelsregister.de/rp_web/search.do发送请求。

请求应如下所示:

Response I need to write in JavaScript

如何添加请求正文?

我的尝试:



function callback(error, response, body) {
	if (error) return error;

	// write the body to an external file
	fs.writeFileSync('body.html', body);

	return body;
}

function writeFile(url = 'https://www.handelsregister.de/rp_web/search.do') {
	const headers = {
    // headers, as you can see in the picture. I am sure my headers a correct.
    
		// this is probably NOT the right place for using a request body, right? At least it does not work ...
		qs:
			'suchTyp=n&registerArt=&registerNummer=&registergericht=R3102&schlagwoerter=&schlagwortOptionen=2&ergebnisseProSeite=100&btnSuche=Find'
	};

	const options = {
		url, // https://www.handelsregister.de/rp_web/search.do
		headers
	};

	return request(options, callback);
}
writeFile().then(() => {
	console.log('finished');
});




2 个答案:

答案 0 :(得分:0)

你应该将它添加到options参数而不是标题,它应该像这样的JSON对象:

request.post({url:'http://service.com/upload', form: {key:'value'}}, function(err,httpResponse,body){ /* ... */ })

看看这里:https://github.com/request/request#forms

答案 1 :(得分:0)

尝试这样的事情。

data= {
key1:value1,
key2:value2,
...
...
}  

request.post({url:' https://www.handelsregister.de/rp_web/search.do',form: data},
function optionalCallback(err, httpResponse, body) {
          if (err) {
            return console.error('request failed:', err);
          }
          console.log('Request successful!  Server responded with:', body);
);