简体   繁体   English

NodeJS - 如何在 POST 请求中发送正文?

[英]NodeJS - how to send body in POST request?

I need to use http internal NodeJS library to make a POST request, how can i attach body object with this request?我需要使用http内部 NodeJS 库来发出POST请求,如何将body object 附加到此请求中? When I'm looking into http.RequestOptions I don't see any data or body property:/当我查看http.RequestOptions时,我没有看到任何databody属性:/

import * as http from "http";

    const options2: http.RequestOptions = {
        hostname: 'www.google.com',
        port: 80,
        path: '/upload',
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
          'Content-Length': Buffer.byteLength(null)
        },
      };
    
    http.request(options, (res: http.IncomingMessage) => {
        
    });

thanks for any help!谢谢你的帮助!

http.request returns an http.ClientRequest object .http.request返回http.ClientRequest object

You need to call the write method on that and pass in your body data.您需要对其调用write方法并传入您的正文数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM