簡體   English   中英

如何在控制台中打印請求正文?

[英]How to print the request body in console?

我有一個javascript代碼,我想向 API 發送一個 http 請求並得到響應,我可以這樣做,但是有沒有辦法可以在控制台中打印我的請求正文?

  <html>
        <body>
            <script>
     // POST request using fetch() 
var user="hello";
    fetch("url", 
    { 

        // Adding method type 
        method: "POST", 

        // Adding body or contents to send 
        body: JSON.stringify({ 
            empUUID : user, 
            body: "bar", 
            userId: 1 ,
            id: id
        }), 

        // Adding headers to the request 
        headers: { 
            "Content-type": "application/json; charset=UTF-8"
        }
    }) 

    // Converting to JSON 
    .then(response => response.json()) 

    // Displaying results to console 
    .then(json => console.log(json)); 


                }

            </script>

        </body>
    </html>

使用變量,然后在收到如下響應時打印您的請求:

<html>
        <body>
            <script>
    // POST request using fetch() 
    var user="hello";
    let request = { 
        empUUID : user, 
        body: "bar", 
        userId: 1 ,
        id: id
    };
    fetch("url", 
    { 

        // Adding method type 
        method: "POST", 

        // Adding body or contents to send 
        body: JSON.stringify(request), 

        // Adding headers to the request 
        headers: { 
            "Content-type": "application/json; charset=UTF-8"
        }
    }) 

    // Converting to JSON 
    .then(response => response.json()) 

    // Displaying results to console 
    .then(json => console.log("request:", request, ", response:", json)); 


                }

            </script>

        </body>
    </html>

暫無
暫無

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

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