博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
node发送post请求_使用Node发出HTTP POST请求
阅读量:2505 次
发布时间:2019-05-11

本文共 1558 字,大约阅读时间需要 5 分钟。

node发送post请求

There are many ways to perform an HTTP POST request in Node, depending on the abstraction level you want to use.

有多种方法可以在Node中执行HTTP POST请求,具体取决于您要使用的抽象级别。

The simplest way to perform an HTTP request using Node is to use the :

使用Node执行HTTP请求的最简单方法是使用 :

const axios = require('axios')axios.post('https://flaviocopes.com/todos', {  todo: 'Buy the milk'}).then((res) => {  console.log(`statusCode: ${res.statusCode}`)  console.log(res)}).catch((error) => {  console.error(error)})

Another way is to use the :

另一种方法是使用 :

const request = require('request')request.post('https://flaviocopes.com/todos', {  json: {    todo: 'Buy the milk'  }}, (error, res, body) => {  if (error) {    console.error(error)    return  }  console.log(`statusCode: ${res.statusCode}`)  console.log(body)})

The 2 ways highlighted up to now require the use of a 3rd party library.

到目前为止突出显示的2种方式都需要使用第三方库。

A POST request is possible just using the Node standard modules, although it’s more verbose than the two preceding options:

POST请求仅使用Node标准模块是可能的,尽管它比前面两个选项更冗长:

const https = require('https')const data = JSON.stringify({  todo: 'Buy the milk'})const options = {  hostname: 'flaviocopes.com',  port: 443,  path: '/todos',  method: 'POST',  headers: {    'Content-Type': 'application/json',    'Content-Length': data.length  }}const req = https.request(options, (res) => {  console.log(`statusCode: ${res.statusCode}`)  res.on('data', (d) => {    process.stdout.write(d)  })})req.on('error', (error) => {  console.error(error)})req.write(data)req.end()

翻译自:

node发送post请求

转载地址:http://yvqgb.baihongyu.com/

你可能感兴趣的文章
分布式系统事务一致性解决方案
查看>>
ShuffleNet总结
查看>>
前后台验证字符串长度
查看>>
《算法导论 - 思考题》7-1 Hoare划分的正确性
查看>>
IOS 简单的动画自定义方法(旋转、移动、闪烁等)
查看>>
图像处理笔记(十二)
查看>>
Chapter 3 Phenomenon——9
查看>>
win64 Python下安装PIL出错解决2.7版本 (3.6版本可以使用)
查看>>
获取各种类型的节点
查看>>
表达式求值-201308081712.txt
查看>>
centos中安装tomcat6
查看>>
从Vue.js窥探前端行业
查看>>
学习进度
查看>>
poj3368 RMQ
查看>>
“此人不存在”
查看>>
github.com加速节点
查看>>
解密zend-PHP凤凰源码程序
查看>>
python3 序列分片记录
查看>>
Atitit.git的存储结构and 追踪
查看>>
atitit 读书与获取知识资料的attilax的总结.docx
查看>>