LucasBeef
LucasBeef

Reputation: 118

Fetch post data after a request in NodeJS

i' m a bit new to Node, so question may be stupid...

I am sending a POST request to a website (through http.request) and I want to be able to use the invisible POST data I get along the response.

I hope this is achievable, and I think so since I am able to preview those data in Chrome debugger.

PS : I understand that we can use BodyParser to parse and get those while listening for a POST call server side, but I have found no example of how to use it coupled with an http.request.

Thanks !

Upvotes: 0

Views: 883

Answers (1)

pbkhrv
pbkhrv

Reputation: 647

If the body of the HTTP response contains JSON, then you need to parse it first in order to turn it from a string into a JavaScript object, like this:

var obj = JSON.parse(body);
console.log(obj.response.auth_token);

More info on various ways of sending a POST request to a server can be found here: How to make an HTTP POST request in node.js?

Edit : So we figured it out in the comments. The value I needed was in the form to begin with, as a hidden field. My error was to think it was generated afterward. So I'll just grab it first then login, so I can use it again for future POST requests on the website.

Upvotes: 1

Related Questions