我们如何在NodeJS中发出这样的HTTP请求?范例或模块.
curl https://www.googleapis.com/urlshortener/v1/url \
-H 'Content-Type: application/json' \
-d '{"longUrl": "http://www.google.com/"}'
我们如何在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/