简体   繁体   中英

How to make an extended http request in Node.js

I want to make an extended request in node.js . I mean, I want to POST data inside another request. Basicaly I want to send a message to a website.

I have this code :

var request = require('request')                                 

request({
    method: "POST",
     baseUrl: "https://www.sitehere.com",
      uri: "/login",
form: { 
      username: "username",
     password: "password",
    autologin: "true"}},
function(err,httpResponse,body){ console.log(body); })

request({
   method: "POST",
    baseUrl: "https://www.sitehere.com",
     uri: "/postmessage",
form: { 
      message: "test"
      }
}, function(err,httpResponse,body){ console.log(body); })

but it does not work because in the 2nd request I am not logged in, I suppose. I am not sure if it is the problem because both requests return body empty. But this works (in browser) :

$.ajax("/login",{data:{
   username:"username",
    password: "password",
     autologin:"true"},
  method:"POST"}).done(postMessage()) // here somehow I get redirected no the home page

function postMessage() {
   $.ajax("/postmessage",{method:"POST",data:{message:"test"}}) 
}

So I want to log in and keep logged to send a message. Sorry for my bad english. Thanks.

Edit : The code above returns body as {success: true, redirectTo: "https://www.sitehere.com/"} and my http post request returns body as null

You ran into the first of many features/problems that might make using nodejs/javascript a little bit confusing in the beginning. You have to move your second call into the closure of the first one, otherwise, the second one will be handled (asynchronously) parallel to the first one instead of waiting for the first one.

var request = require('request')                                 
const cookieJar = request.jar()

request({
    jar: cookieJar, 
    method: "POST",
    baseUrl: "https://www.sitehere.com",
    uri: "/login",
    form: { 
      username: "username",
      password: "password",
      autologin: "true"}
    },function(err,httpResponse,body){ 
     console.log(body); 
     request({
      jar: cookieJar, 
      method: "POST",
      baseUrl: "https://www.sitehere.com",
      uri: "/postmessage",
      form: { 
      message: "test"
      }
    }, function(err,httpResponse,body){ console.log(body); })
})

To make your life a lot easier, I'd recommend you first have a look at how nodejs handles asynchronicity, what that means in connection with closures and how to avoid callback hell using promises through the more up-to-date async/await syntax.

Update: As @Brad noted in the comments, you might also want to persist the cookies from the request which is why I added a cookie jar.

您可以尝试使用sync-request

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM