0

I am trying to use Axios, because it's the only module with async/await feature with NodeJS.

I already have a POST request in Python script, which work very well, and I am trying to adapt on NodeJS server.

But I block on my first post request. If i use with Request module directly, it's work very well, see my code :

console.log("create session");
let data = {'uname': 'login', 'pass':  'password', 'module':'NSUser', 'op':'login','url':'%2Findex.php'}
let url = 'https://localhost:9000/user.php'
let headers = {'Accept': "application/html"}

//s = request.session
body = await request.post({url:url, headers:headers, form:data}, function(error, response, body){
  console.log("LOGIN");
 console.log(response);

});

With Axios (or Axios instance but it's for later...) because i will need to keep my session (like in Python it work like a charm).

     const config = {
      method: 'post',
      url: 'https://localhost:9000/user.php',
      headers:  {'content-type': 'application/x-www-form-urlencoded'},
      data: {uname:'login',
        pass:'password',
        module:'NSUser',
        op:'login',
        url:'/index.php'}
     }    
    axios(config)

And the body in response, it's not connecting page than the code with Request.

What i am wrong ? I try to replace data by param, but it's same....

I have CURL command from Firefox if i need.

For information, in python :

s = requests.session()
res = s.post(url, headers=headers, data=data)

and my s variable have the session, and can request everything as connected.

Amaresh S M
  • 2,936
  • 2
  • 13
  • 25
  • See this thread. You need to use URLSearchParams when sending x-www-form-urlencoded: https://stackoverflow.com/questions/41457181/axios-posting-params-not-read-by-post – Loïc Bellemare-Alford Aug 16 '19 at 22:54
  • Also in `await request()`, the `await` doesn't do anything useful. `await` only does something useful with promises. If you want version of `request()` that returns promise, see the `request-promise` module. – jfriend00 Aug 16 '19 at 23:28

1 Answers1

1

You say you are sending form-encoded data:

headers:  {'content-type': 'application/x-www-form-urlencoded'},

… but you pass an object to Axios:

data: {uname:'login', …

… so Axios will encode it as JSON and not as form-encoded data.


See the documentation:

By default, axios serializes JavaScript objects to JSON. To send data in the application/x-www-form-urlencoded format instead, you can use one of the following options.

In node.js, you can use the querystring module as follows:

const querystring = require('querystring');
axios.post('http://something.com/', querystring.stringify({ foo: 'bar' }));
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • I didn't see this information area. So i changed, but it doesn't work, the result is same... I already tested with new FormData(), no result... – user3216533 Aug 19 '19 at 08:48