我们如何在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相关问答推荐

如果非SQL函数在事务内部运行失败,PG-Promise事务会回滚吗?

对于具有重叠列的组合键,在冲突&q;上没有唯一或排除约束匹配错误

我的Node.js应用程序没有将Mongoose方法findByIdAndDelete作为函数进行检测

在内存中加载安全密钥安全吗?还是每次都从文件中读取?

mongodb首先自连接,然后根据某些条件与另一个表连接

MongoDB:通过匹配输入字符串或输入字符串中的单个单词进行搜索

Solidity 将数据位置从内存更改为存储

nyc 代码覆盖不适用于 NodeJs Express 服务器

express app.post的多个参数在Node.js中的定义是什么

当我try 从本地主机发布新产品时收到错误消息

如何删除mongodb中嵌套数组中所有出现的数组元素

[NodeJs 从 ADAL 升级到 MSAL]:无法在字符串上创建属性authenticationScheme

Azure Function 在 2022 年 4 月 26 日禁用将用户字段设置为 HTTP 请求对象

cURL 和 shell 任务

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

如何以编程方式检测nodejs中的调试模式?

如何为 node.js 服务器分配域名?

在 Jade 包含中使用变量

要求('babel/register')不起作用

Google Firebase 错误(函数返回未定义、预期的 Promise 或值)