I'm having a problem in posting data in node.js with Content-type: 'application/x-www-form-urlencoded'
var loginArgs = { data: 'username="xyzzzzz"&"password="abc12345#"', //data: { // 'username': "xyzzzzz", // 'password': "abc12345#", //}, headers: { 'User-Agent': 'MYAPI', 'Accept': 'application/json', 'Content-Type':'application/x-www-form-urlencoded' } };
And post request is:
client.post("http:/url/rest/login", loginArgs, function(data, response){ console.log(loginArgs); if (response.statusCode == 200) { console.log('succesfully logged in, session:', data.msg); }
It always returns username/password incorrect.
In the rest api it is said that the request body should be:
username='provide user name in url encoded format'&password= "provide password in url encoded format'
To send data in the application/x-www-form-urlencoded format instead, you can use one of the following options. In a browser, you can use the URLSearchParams API as follows: const params = new URLSearchParams(); params. append('param1', 'value1'); params.
var obj = { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', }, }; fetch('https://example.com/login', obj) . then(function(res) { // Do stuff with result });
request
supports application/x-www-form-urlencoded
and multipart/form-data
form uploads. For multipart/related
refer to the multipart API.
application/x-www-form-urlencoded (URL-Encoded Forms)
URL-encoded forms are simple:
const request = require('request'); request.post('http:/url/rest/login', { form: { username: 'xyzzzzz', password: 'abc12345#' } }) // or request.post('http:/url/rest/login').form({ username: 'xyzzzzz', password: 'abc12345#' }) // or request.post({ url: 'http:/url/rest/login', form: { username: 'xyzzzzz', password: 'abc12345#' } }, function (err, httpResponse, body) { /* ... */ })
See: https://github.com/request/request#forms
Or, using request-promise
const rp = require('request-promise'); rp.post('http:/url/rest/login', { form: { username: 'xyzzzzz', password: 'abc12345#' } }).then(...);
See: https://github.com/request/request-promise#api-in-detail
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With