nodejs post请求
Posted 影响力
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nodejs post请求相关的知识,希望对你有一定的参考价值。
const http = require(‘http‘); const querystring = require(‘querystring‘); const postData = querystring.stringify({ ‘msg‘: ‘Hello World! ----- 哈利路亚‘ }); const options = { hostname: ‘192.168.1.6‘, port: 8080, path: ‘/hello‘, method: ‘POST‘, headers: { ‘Content-Type‘: ‘application/x-www-form-urlencoded‘, ‘Content-Length‘: Buffer.byteLength(postData) } }; const req = http.request(options, (res) => { console.log(`STATUS: ${res.statusCode}`); console.log(`HEADERS: ${JSON.stringify(res.headers)}`); res.setEncoding(‘utf8‘); res.on(‘data‘, (chunk) => { console.log(`BODY: ${chunk}`); }); res.on(‘end‘, () => { console.log(‘No more data in response.‘); }); }); req.on(‘error‘, (e) => { console.error(`problem with request: ${e.message}`); }); // write data to request body req.write(postData); req.end();
以上是关于nodejs post请求的主要内容,如果未能解决你的问题,请参考以下文章
NodeJS - 只有 OPTIONS 请求被发送到 REST API,POST 或 GET 不跟随(Cloudflare)