我们如何在NodeJS中发出这样的HTTP请求?示例或模块的赞赏。
curl https://www.googleapis.com/urlshortener/v1/url \ -H 'Content-Type: application/json' \ -d '{"longUrl": "http://www.google.com/"}'
Mikeal的请求 模块可以轻松做到这一点:
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. } });