javascript - 如何在控制台中打印请求正文?
问题描述
我有一个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>
推荐阅读
- python-3.x - 当我尝试在虚拟环境中通过“pip3 install translate”安装包(翻译)时出现错误
- php - Eloquent API 资源 - 将资产链接添加到嵌套值
- javascript - Mongoose 找不到所需的输出
- python - (已解决)Tensorflow 联合 | tff.learning.from_keras_model() 具有具有 DenseFeature 层和多个输入的模型
- cassandra - Apache Cassandra 3.11.4 是否支持每个节点超过 1TB 的存储?
- java - 在flyway java迁移中检索属性
- javascript - 使用不同的变量调用相同的函数 - 简化代码
- python - MATLAB和python中BCH代码的区别
- c# - 在 ListView Winform 中显示数据库中的多个列
- php - 如何将 SQS 延迟时间增加到 900 秒以上