node.js - 发送 Content-Type : application/json post with node. js

标签 node.js post curl

我们如何在 NodeJS 中发出这样的 HTTP 请求?示例或模块赞赏。

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

最佳答案

Mikeal's request模块可以轻松做到这一点:

var request = require('request');

var options = {
  uri: 'https://www.googleapis.com/urlshortener/v1/url',
  method: 'POST',
  json: {
    "longUrl": "http://www.google.com/"
  }
};

request(options, function (error, response, body) {
  if (!error && response.statusCode == 200) {
    console.log(body.id) // Print the shortened url.
  }
});

关于node.js - 发送 Content-Type : application/json post with node. js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8675688/

相关文章:

php - 页面关闭后继续 php exec() ffmpeg

curl - 为什么 curl 不能下载 ffmpeg-2.7.ta​​r.bz2?

php - 使用 PHP cURL 登录 eBay

node.js - Webpack构建在生产环境上

jquery - Web API OWIN 从 $.AJAX POST withCredentials :true 接收空数据

Spring MVC - 为什么不能同时使用 @RequestBody 和 @RequestParam

maven - Jenkins 管道未完成 tomcat war 部署

javascript - 错误无法使用node.js和mysql读取未定义的属性

javascript - 更新角色一次仅允许用户使用一个角色

node.js - 为什么只有 50 个实例在 AWS lambda 上运行?