0

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 !

LucasBeef
  • 118
  • 2
  • 9
  • are you using Express or a straight up node http server? – pbkhrv Jan 13 '15 at 00:17
  • I didn' t plan on using express. I don't think I need it since what I want is to make a robot (not the bad kind) to automate an action on an external website. – LucasBeef Jan 13 '15 at 01:19
  • Does this possibly related SO question help? http://stackoverflow.com/questions/6158933/how-to-make-an-http-post-request-in-node-js – pbkhrv Jan 13 '15 at 03:02
  • That would be the first step, to send a POST request out. The second step (where I' m stuck) is to recover the POST data in the response I get. Let's say instead of doing a console.log(body) in the callback, I want to do something like console.log(body.response.some-post-input). – LucasBeef Jan 13 '15 at 03:44
  • to clarify something: "recover the POST data in the response I get" - by "POST data", do you mean data sent to the server as part of the request, or data received from server as part of the response? The only reason I'm asking is because you are mentioning "body.response.some-post-input", which is making me think that you are trying to access some part of the request from your response? – pbkhrv Jan 13 '15 at 04:10
  • I mean data received from the server as part of the response. Sorry, I' m gonna be more explicit. So I want to login to a website in order to do some other POST requests afterward. The thing is, it uses some kind of authentification token which is needed in any other POST request I'll want to make. This token is generated (at login) somehow and can be found in the POST data of the response (along with my username and password). So the question is : How to get this token ? – LucasBeef Jan 13 '15 at 04:50

1 Answers1

1

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.

Community
  • 1
  • 1
pbkhrv
  • 647
  • 4
  • 11
  • The response is HTML, so JSON.parse is not happy. When i console.log the body i get a short html string telling me I am being redirected, but no sign of the token. Although I can see it using Chrome's debugger... – LucasBeef Jan 13 '15 at 16:12
  • do you see the same redirect happening in the Chrome debugger? which part of the response is the token part of - the body or one of the headers? – pbkhrv Jan 13 '15 at 16:16
  • When i click login, it calls /session/create which gives me a status 302. The username, password and the token can be found in the debugger under Headers->Form Data. – LucasBeef Jan 13 '15 at 19:32
  • Since your token is in "Headers->Form Data", that probably means that token is generated on the server when the form HTML is generated, NOT as a response to the POST. Look at the source of the page with the login form on it in Chrome - do you see a token field there? – pbkhrv Jan 13 '15 at 20:54
  • Oh yeah, I think we got it ! So yeah, it's in the source code. It's actually generated before I log in, as a hidden field of the form. So I'll just need to grab it first thing then I can log in and use it again ! Thanks man, I was looking at it from the wrong perspective ! – LucasBeef Jan 13 '15 at 21:28