发送Content-Type:application / json post和node.js [英] send Content-Type: application/json post with node.js

查看:759
本文介绍了发送Content-Type:application / json post和node.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何在NodeJS中发出类似这样的HTTP请求?示例或模块感谢。

How can we make a HTTP request like this in NodeJS? Example or module appreciated.

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


推荐答案

Mikeal的请求 模块可以轻松完成此操作:

Mikeal's request module can do this easily:

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.
  }
});

这篇关于发送Content-Type:application / json post和node.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆