Используйте тело запроса с requestjs - PullRequest
0 голосов
/ 10 июня 2018

Я использую requestjs для отправки запроса на https://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 голосов
/ 10 июня 2018

Попробуйте что-то вроде этого.

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);
);
0 голосов
/ 10 июня 2018

Вы должны добавить его к аргументу options вместо заголовков, и он должен быть в виде объекта JSON, например:

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

Взгляните здесь: https://github.com/request/request#forms

...