如何使用会话数据执行POST请求到服务器节点js中的端点

时间:2021-09-17 20:41:13

I am working on express and I need to perform a POST request to an endpoint within the server. My code for this is :

我正在使用express,我需要对服务器内的端点执行POST请求。我的代码是:

request({
  url : 'http://localhost:3000/api/oauth2/authorize',
  qs:{
    transaction_id:req.oauth2.transactionID,
    user:req.user,
    client : req.oauth2.client
  },
  headers:{
    'Authorization':auth,
     'Content-Type':'application/x-www-form-urlencoded'
  },
  method:'POST'
},function(err,res,bo){
  console.log("Got response with body :"+bo);
});      

localhost is the current server, this works properly but the session data is lost when i perform the POST request.
Is there any other way to perform a POST within the same server or to save the session data such that it is maintained after the POST?

localhost是当前服务器,这可以正常工作,但是当我执行POST请求时会话数据丢失。是否有任何其他方法可以在同一服务器中执行POST或保存会话数据,以便在POST后维护它?

1 个解决方案

#1


0  

Well, typically you register your routes something like:

好吧,通常你注册的路线如下:

var handlePostRequest = function(req,res,next) {
    // process req.body etc.
};
app.post('/api/oauth2/authorize', handlePostRequest);

If you want to call that endpoint from within your application, you simply call handlePostRequest() providing the req, res, next objects as well.

如果要从应用程序中调用该端点,只需调用handlePostRequest()即可提供req,res,next对象。

Assuming handlePostRequest is in global scope, or required already; in your example that would be:

假设handlePostRequest在全局范围内,或者已经需要;在你的例子中将是:

app.get('/some/other/endpoint', function(req,res,next){
    // override the default req.body with your supplied data
    req.body = {
        transaction_id: req.oauth2.transactionID,
        user: req.user,
        client: req.oauth2.client
    };

    // you may also override req.headers etc. for authorization
    // ...

    // then call the "api" again with the new values
    return handlePostRequest(req,res,next);
});

IF you however strictly want to make a POST request (for some reason), you need to supply the sessionID as well, which will be in your cookie. Then the session data will be available.

如果您严格要发出POST请求(由于某种原因),您还需要提供sessionID,它将在您的cookie中。然后会话数据将可用。

#1


0  

Well, typically you register your routes something like:

好吧,通常你注册的路线如下:

var handlePostRequest = function(req,res,next) {
    // process req.body etc.
};
app.post('/api/oauth2/authorize', handlePostRequest);

If you want to call that endpoint from within your application, you simply call handlePostRequest() providing the req, res, next objects as well.

如果要从应用程序中调用该端点,只需调用handlePostRequest()即可提供req,res,next对象。

Assuming handlePostRequest is in global scope, or required already; in your example that would be:

假设handlePostRequest在全局范围内,或者已经需要;在你的例子中将是:

app.get('/some/other/endpoint', function(req,res,next){
    // override the default req.body with your supplied data
    req.body = {
        transaction_id: req.oauth2.transactionID,
        user: req.user,
        client: req.oauth2.client
    };

    // you may also override req.headers etc. for authorization
    // ...

    // then call the "api" again with the new values
    return handlePostRequest(req,res,next);
});

IF you however strictly want to make a POST request (for some reason), you need to supply the sessionID as well, which will be in your cookie. Then the session data will be available.

如果您严格要发出POST请求(由于某种原因),您还需要提供sessionID,它将在您的cookie中。然后会话数据将可用。