簡體   English   中英

在POST請求觸發之前,如何獲取GET請求的響應主體的內容? 能做到這一點嗎?

[英]How do I get the contents of the response body of a GET request before the POST request fires? Can this be done with promise?

因此,我從前端接收JSON對象,並將其發送到后端的端點/ getData。 在這里,我使用GET請求從API獲取數據,並且需要在最終的POST請求中發送JSON對象和GET響應主體。 但是,當我發送請求時,GET響應的主體來不及,並且“ formInfo”未定義。

如何解決此問題,以便GET完成后發送POST請求?

 app.post('/getData', function(req, res) {
    debugger;
    var data = req.body;
    console.log(data);
    toSend = data;
    res.send({msg: "Success"});
    var findID = {};
    var endPoint = 'https://secure.p01.eloqua.com/API/REST/2.0/assets/form/' + toSend["formID"].toString();
    var options = {
        method: "GET",
        headers: {'Authorization': authenticationHeader, 'Content-Type': 'application/json'}
    };
    request.get(endPoint, options, function (error, response, body) {
        console.log(body);
        findID = body["elements"];
            request({
                    method: "POST",
                    headers: {'content-type': 'application/json', 'authorization': authenticationHeader},
                    url: 'http://localhost:3000/handleData',
                    json: {
                        "tuples": toSend,
                        "formInfo": body['elements']
                    }},
                function (error, response, body) {
                    console.log(response);
                });
    });
});

您是否考慮過使用fetch API

它在現代瀏覽器中得到廣泛的支持 -您甚至可以將其轉換為最糟糕的情況-以及NodeJS。

有了它,您就可以鏈接請求,例如

fetch(`first.url.com`)
  .then(response => 
    fetch(`second.url.com`, {body: response.body})
  )

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM