javascript - Need help to induction curl Post in code JS / Node.js
问题描述
curl -X POST -H 'Content-type: application/json' --data '{"text":"Hello, World!"}'....
It works if I use this in the console. How can I implement this in JS code?
解决方案
除非我对 curl 有什么遗漏,否则您可以使用 fetch 发出类似的请求:
fetch(urlToPostTo, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({text: 'Hello, World!'})
});
推荐阅读
- python - 测试单元在 TDD 中的效率
- django - 如何在 form.save() 之前限制重复值插入表中
- python - Visdom 什么也没显示
- php - 哪个 ODBC 驱动程序 (x86/x64) 用于 php 7.2 - xampp?
- database - 通过 id 从另一个表中获取值
- apache-flink - 如何跟踪 flink 中的延迟?
- opengl - 为什么将OpenGL色枪uint8转换为浮点数时除以255而不是更快的256?255 的 1.0f 而不是 0.996f 至关重要吗?
- r - 为什么会出现下标越界错误?
- vue.js - 如何修复未使用的 vuejs env 变量
- android - 在 android 应用程序中替换="android:appComponentFactory"