Search code examples
node.jspostman

postman/post shows my request does not contain form-data. Need help constructing the request


My node.js module constructs a post request with form-data.

When I test this on postman-echo.com/post postman-echo returns my request but only the headers that I had setup are in the postman-echo response. The form-data is nowhere to be seen.

Set up the module with: import { request } from 'https'; import FormData from 'form-data';

Create a new FormData object and append a few fields.

Create an options object that contains headers and body: form-data.

Invoke request with option object as parameter.

Log response from postman-echo/post.

Response indicates the headers were picked up, but not the form-data.

code (updated per Hashila suggestion 2023/03/31 13:00 est):

import { request } from 'https';
import FormData from 'form-data';

const formData = new FormData();
formData.append('type', '1');
formData.append('subject', 'Example subject');
formData.append('description', 'Example description');
formData.append('location', 'Example location');
console.log("🚀 ~ file: postman-post.mjs:9 ~ formData:", formData)
var boundary = formData._boundary;
console.log("🚀 ~ file: postman-post.mjs:11 ~ boundary:", boundary);

const options = {
  hostname: 'postman-echo.com',
  port: 443,
  path: '/post',
  method: 'POST',
  headers: {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36',
    'Cookie': 'myCookie=myCookiee',
    'Content-Type': `multipart/form-data; boundary=${boundary}`
  },
  body: formData
};

const req = request(options, (res) => {
  console.log(`Status code: ${res.statusCode}`);

  res.on('data', (chunk) => {
    console.log(`Response body: ${chunk}`);
  });
});

req.on('error', (error) => {
  console.error(error);
});

req.end();

This is the console log:

🚀 ~ file: postman-post.mjs:9 ~ formData: FormData {
  _overheadLength: 426,
  _valueLength: 51,
  _valuesToMeasure: [],
  writable: false,
  readable: true,
  dataSize: 0,
  maxDataSize: 2097152,
  pauseStreams: true,
  _released: false,
  _streams: [
    '----------------------------511827528311442099887851\r\n' +
      'Content-Disposition: form-data; name="type"\r\n' +
      '\r\n',
    '1',
    [Function: bound ],
    '----------------------------511827528311442099887851\r\n' +
      'Content-Disposition: form-data; name="subject"\r\n' +
      '\r\n',
    'Example subject',
    [Function: bound ],
    '----------------------------511827528311442099887851\r\n' +
      'Content-Disposition: form-data; name="description"\r\n' +
      '\r\n',
    'Example description',
    [Function: bound ],
    '----------------------------511827528311442099887851\r\n' +
      'Content-Disposition: form-data; name="location"\r\n' +
      '\r\n',
    'Example location',
    [Function: bound ]
  ],
  _currentStream: null,
  _insideLoop: false,
  _pendingNext: false,
  _boundary: '--------------------------511827528311442099887851'
}
🚀 ~ file: postman-post.mjs:11 ~ boundary: --------------------------511827528311442099887851
Status code: 200
Response body: {
  "args": {},
  "data": {},
  "files": {},
  "form": {},
  "headers": {
    "x-forwarded-proto": "https",
    "x-forwarded-port": "443",
    "host": "postman-echo.com",
    "x-amzn-trace-id": "Root=1-64270c8e-1e3b458b7da07d124d6829ce",
    "content-length": "0",
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36",
    "cookie": "myCookie=myCookiee",
    "content-type": "multipart/form-data; boundary=--------------------------511827528311442099887851"
  },
  "json": null,
  "url": "https://postman-echo.com/post"

Response body: }

Solution

  • There is no body option for the request call. You need to use req.write.

    req.write(formData.getBuffer());
    

    There is also no need to make the Content-Type header manually. Use formData.getHeaders().

    headers: {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36',
        'Cookie': 'myCookie=myCookiee',
        ...formData.getHeaders(),
    },