I am trying to migrate from request to axios, since request has been deprecated.
Suppose that the url 'https://www.example.com' receives a post request with formdata that contains login information, and prints 'Logged in' on success and 'Could not log in' otherwise(I can't share the url for privacy reasons).
I have the following code, which uses axios and prints 'Could not log in':
axios = require('axios')
FormData = require('form-data')
form = new FormData()
form.append('email', 'example@gmail.com')
form.append('password', '1234')
axios({
method: 'post',
url: 'https://www.example.com',
data: form
}).then(function (response) {
console.log(response['data']); // Prints "Could not log in"
}).catch(function (error) {
console.log(error);
})
I also have the following code, which uses request and prints 'Logged in':
request = require('request')
request.post({
url: 'https://www.example.com',
method: 'POST',
formData: {
'email': 'example@gmail.com',
'password': '1234'
}
}, function(error, response, body) {
console.log(body); // Prints "Logged in"
})
Why does the operation work using request but not axios?
Here is the output of the Request code:
content-length: 288
content-type: multipart/form-data; boundary=--------------------------539399892261259576142530
----------------------------539399892261259576142530
Content-Disposition: form-data; name="email"
example@gmail.com
----------------------------539399892261259576142530
Content-Disposition: form-data; name="password"
1234
----------------------------539399892261259576142530--
And here is the output of the Axios code:
content-length: 288",
accept: application/json, text/plain, */*
content-type: application/x-www-form-urlencoded
user-agent: "axios/0.19.2
----------------------------076596858609798080293678
Content-Disposition: form-data; name="email"
example@gmail.com
----------------------------076596858609798080293678
Content-Disposition: form-data; name="password"
1234
----------------------------076596858609798080293678--
Try adding this option in Axios: headers: {'Content-Type': `multipart/form-data; boundary=${form._boundary}` }