postman/post显示我的请求不包含表单数据。需要帮助构建请求。

huangapple go评论67阅读模式
英文:

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

问题

我的Node.js模块构建了一个带有表单数据的POST请求。

当我在postman-echo.com/post上测试它时,postman-echo返回了我的请求,但响应中只包含我设置的头部信息,表单数据并未出现。

模块设置如下:
import { request } from 'https';
import FormData from 'form-data';

创建一个新的FormData对象并附加一些字段。

创建一个包含头部和body(form-data)的选项对象。

使用选项对象调用请求。

记录postman-echo/post的响应。

响应表明头部已被识别,但表单数据未被包括。

更新后的代码(根据Hashila建议于2023年3月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("formData:", formData);
var boundary = formData._boundary;
console.log("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();

这是控制台日志:

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'
}
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"
}

英文:

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: }

答案1

得分: 1

没有request调用的body选项。您需要使用req.write

req.write(formData.getBuffer());

也不需要手动设置Content-Type标头。使用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(),
},
英文:

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(),
},

huangapple
  • 本文由 发表于 2023年3月31日 19:03:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75897800.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定