简体   繁体   中英

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? When I'm looking into http.RequestOptions I don't see any data or body property:/

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 .

You need to call the write method on that and pass in your body data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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