我们如何在NodeJS中发出这样的HTTP请求?范例或模块.

curl https://www.googleapis.com/urlshortener/v1/url \
  -H 'Content-Type: application/json' \
  -d '{"longUrl": "http://www.google.com/"}'

推荐答案

100 is Lesser and Better:

const data = JSON.stringify({
  message: 'Hello World!'
})

const url = "https://localhost/WeatherAPI";

axios({
    method: 'POST',
    url, 
    data: JSON.stringify(data), 
    headers:{'Content-Type': 'application/json; charset=utf-8'}
}) 
  .then((res) => {
    console.log(`statusCode: ${res.status}`)
    console.log(res)
  })
  .catch((error) => {
    console.error(error)
  })

还要判断5 Ways to Make HTTP Requests in Node.js

https://www.twilio.com/blog/2017/08/http-requests-in-node-js.html

Refer:

https://nodejs.dev/learn/make-an-http-post-request-using-nodejs

https://flaviocopes.com/node-http-post/

https://stackabuse.com/making-asynchronous-http-requests-in-javascript-with-axios/

Node.js相关问答推荐

如何在Node.js 中设置图表js的背景色

Mongoose查询-如何根据当前查找ID获取其他集合并将其插入到当前查找中?

从mongodb集合中获取每分钟数据的每小时数据

将 POST 的 json 变量格式化为 lambda

try 使用 pdf.js 时 pdf.getPage 不是函数

密码加密的最佳实践是什么?

登录用户并获取他们的个人资料

级联定时器和Jest 的异步功能

try 在 NodeJS 项目中实现 SimplePay (OTP) 支付网关,但我无法获得与技术文档中相同的签名

如何使用 Jest 模拟异步函数的延迟时间

Node.js 大文件上传到 MongoDB 阻塞了事件循环和工作池

自定义 Docker 容器 Github 操作无法在 /github/workspace 中找到 Node 脚本

Web3.js 脚本在监听 CreatedPairs 时退出

在 Passport 策略回调中获取请求对象

Handlebars:访问已被拒绝解析来自的属性,因为它不是其父级的自己的属性

如何可靠地散列 JavaScript 对象?

编写可维护的事件驱动代码

npm install 给出警告,npm audit fix 不起作用

expressjs app.VERB 调用中的 next() 和 next('route') 有什么区别?

NodeJS:如何调试检测到 EventEmitter 内存泄漏.添加了 11 个侦听器