问题
I have code like the following:
var req = require('request');
req.post('someUrl',
{ form: { username: 'user', password: '', opaque: 'someValue', logintype: '1'}, },
function (e, r, body) {
console.log(body);
});
How can I set headers for this? I need user-agent, content-type and probably something else to be in the headers:
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.110 Safari/537.36',
'Content-Type' : 'application/x-www-form-urlencoded'
};
I've tried in multiple ways but I can either send header or form-data, failed to send both.
回答1:
I've finally managed to do it. Answer in code snippet below:
var querystring = require('querystring');
var request = require('request');
var form = {
username: 'usr',
password: 'pwd',
opaque: 'opaque',
logintype: '1'
};
var formData = querystring.stringify(form);
var contentLength = formData.length;
request({
headers: {
'Content-Length': contentLength,
'Content-Type': 'application/x-www-form-urlencoded'
},
uri: 'http://myUrl',
body: formData,
method: 'POST'
}, function (err, res, body) {
//it works!
});
回答2:
This should work.
var url = 'http://<your_url_here>';
var headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Firefox/24.0',
'Content-Type' : 'application/x-www-form-urlencoded'
};
var form = { username: 'user', password: '', opaque: 'someValue', logintype: '1'};
request.post({ url: url, form: form, headers: headers }, function (e, r, body) {
// your callback body
});
回答3:
I think it's just because you have forgot HTTP METHOD. The default HTTP method of request is GET.
You should add method: 'POST'
and your code will work if your backend receive the post method.
var req = require('request');
req.post({
url: 'someUrl',
form: { username: 'user', password: '', opaque: 'someValue', logintype: '1'},
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.110 Safari/537.36',
'Content-Type' : 'application/x-www-form-urlencoded'
},
method: 'POST'
},
function (e, r, body) {
console.log(body);
});
回答4:
I found the solution of this problem and i should work i'm sure about this because i also face the same problem
here is my solution----->
var request = require('request');
//set url
var url = 'http://localhost:8088/example';
//set header
var headers = {
'Authorization': 'Your authorization'
};
//set form data
var form = {first_name: first_name, last_name: last_name};
//set request parameter
request.post({headers: headers, url: url, form: form, method: 'POST'}, function (e, r, body) {
var bodyValues = JSON.parse(body);
console.log(updatedValues.first_name);
console.log("====================================");
console.log('error:', e); // Print the error if one occurred
console.log('statusCode:', r && r.statusCode); // Print the response status code if a response was received
console.log('body:', body); // Print the HTML for the Google homepage.
console.log("====================================");
res.send(bodyValues);
});
回答5:
Just remember set method to POST in options. Here is my code
var options = {
url: 'http://www.example.com',
method: 'POST', // Don't forget this line
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'X-MicrosoftAjax': 'Delta=true', // blah, blah, blah...
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36',
},
form: {
'key-1':'value-1',
'key-2':'value-2',
...
}
};
//console.log('options:', options);
// Create request to get data
request(options, (err, response, body) => {
if (err) {
//console.log(err);
} else {
console.log('body:', body);
}
});
来源:https://stackoverflow.com/questions/17121846/node-js-how-to-send-headers-with-form-data-using-request-module