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

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

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

  1. import { request } from 'https';
  2. import FormData from 'form-data';
  3. const formData = new FormData();
  4. formData.append('type', '1');
  5. formData.append('subject', 'Example subject');
  6. formData.append('description', 'Example description');
  7. formData.append('location', 'Example location');
  8. console.log("formData:", formData);
  9. var boundary = formData._boundary;
  10. console.log("boundary:", boundary);
  11. const options = {
  12. hostname: 'postman-echo.com',
  13. port: 443,
  14. path: '/post',
  15. method: 'POST',
  16. headers: {
  17. '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',
  18. 'Cookie': 'myCookie=myCookiee',
  19. 'Content-Type': `multipart/form-data; boundary=${boundary}`
  20. },
  21. body: formData
  22. };
  23. const req = request(options, (res) => {
  24. console.log(`Status code: ${res.statusCode}`);
  25. res.on('data', (chunk) => {
  26. console.log(`Response body: ${chunk}`);
  27. });
  28. });
  29. req.on('error', (error) => {
  30. console.error(error);
  31. });
  32. req.end();

这是控制台日志:

  1. formData: FormData {
  2. _overheadLength: 426,
  3. _valueLength: 51,
  4. _valuesToMeasure: [],
  5. writable: false,
  6. readable: true,
  7. dataSize: 0,
  8. maxDataSize: 2097152,
  9. pauseStreams: true,
  10. _released: false,
  11. _streams: [
  12. '----------------------------511827528311442099887851\r\n' +
  13. 'Content-Disposition: form-data; name="type"\r\n' +
  14. '\r\n',
  15. '1',
  16. [Function: bound ],
  17. '----------------------------511827528311442099887851\r\n' +
  18. 'Content-Disposition: form-data; name="subject"\r\n' +
  19. '\r\n',
  20. 'Example subject',
  21. [Function: bound ],
  22. '----------------------------511827528311442099887851\r\n' +
  23. 'Content-Disposition: form-data; name="description"\r\n' +
  24. '\r\n',
  25. 'Example description',
  26. [Function: bound ],
  27. '----------------------------511827528311442099887851\r\n' +
  28. 'Content-Disposition: form-data; name="location"\r\n' +
  29. '\r\n',
  30. 'Example location',
  31. [Function: bound ]
  32. ],
  33. _currentStream: null,
  34. _insideLoop: false,
  35. _pendingNext: false,
  36. _boundary: '--------------------------511827528311442099887851'
  37. }
  38. boundary: --------------------------511827528311442099887851
  39. Status code: 200
  40. Response body: {
  41. "args": {},
  42. "data": {},
  43. "files": {},
  44. "form": {},
  45. "headers": {
  46. "x-forwarded-proto": "https",
  47. "x-forwarded-port": "443",
  48. "host": "postman-echo.com",
  49. "x-amzn-trace-id": "Root=1-64270c8e-1e3b458b7da07d124d6829ce",
  50. "content-length": "0",
  51. "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",
  52. "cookie": "myCookie=myCookiee",
  53. "content-type": "multipart/form-data; boundary=--------------------------511827528311442099887851"
  54. },
  55. "json": null,
  56. "url": "https://postman-echo.com/post"
  57. }
英文:

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

  1. import { request } from 'https';
  2. import FormData from 'form-data';
  3. const formData = new FormData();
  4. formData.append('type', '1');
  5. formData.append('subject', 'Example subject');
  6. formData.append('description', 'Example description');
  7. formData.append('location', 'Example location');
  8. console.log("🚀 ~ file: postman-post.mjs:9 ~ formData:", formData)
  9. var boundary = formData._boundary;
  10. console.log("🚀 ~ file: postman-post.mjs:11 ~ boundary:", boundary);
  11. const options = {
  12. hostname: 'postman-echo.com',
  13. port: 443,
  14. path: '/post',
  15. method: 'POST',
  16. headers: {
  17. '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',
  18. 'Cookie': 'myCookie=myCookiee',
  19. 'Content-Type': `multipart/form-data; boundary=${boundary}`
  20. },
  21. body: formData
  22. };
  23. const req = request(options, (res) => {
  24. console.log(`Status code: ${res.statusCode}`);
  25. res.on('data', (chunk) => {
  26. console.log(`Response body: ${chunk}`);
  27. });
  28. });
  29. req.on('error', (error) => {
  30. console.error(error);
  31. });
  32. req.end();

This is the console log:

  1. 🚀 ~ file: postman-post.mjs:9 ~ formData: FormData {
  2. _overheadLength: 426,
  3. _valueLength: 51,
  4. _valuesToMeasure: [],
  5. writable: false,
  6. readable: true,
  7. dataSize: 0,
  8. maxDataSize: 2097152,
  9. pauseStreams: true,
  10. _released: false,
  11. _streams: [
  12. '----------------------------511827528311442099887851\r\n' +
  13. 'Content-Disposition: form-data; name="type"\r\n' +
  14. '\r\n',
  15. '1',
  16. [Function: bound ],
  17. '----------------------------511827528311442099887851\r\n' +
  18. 'Content-Disposition: form-data; name="subject"\r\n' +
  19. '\r\n',
  20. 'Example subject',
  21. [Function: bound ],
  22. '----------------------------511827528311442099887851\r\n' +
  23. 'Content-Disposition: form-data; name="description"\r\n' +
  24. '\r\n',
  25. 'Example description',
  26. [Function: bound ],
  27. '----------------------------511827528311442099887851\r\n' +
  28. 'Content-Disposition: form-data; name="location"\r\n' +
  29. '\r\n',
  30. 'Example location',
  31. [Function: bound ]
  32. ],
  33. _currentStream: null,
  34. _insideLoop: false,
  35. _pendingNext: false,
  36. _boundary: '--------------------------511827528311442099887851'
  37. }
  38. 🚀 ~ file: postman-post.mjs:11 ~ boundary: --------------------------511827528311442099887851
  39. Status code: 200
  40. Response body: {
  41. "args": {},
  42. "data": {},
  43. "files": {},
  44. "form": {},
  45. "headers": {
  46. "x-forwarded-proto": "https",
  47. "x-forwarded-port": "443",
  48. "host": "postman-echo.com",
  49. "x-amzn-trace-id": "Root=1-64270c8e-1e3b458b7da07d124d6829ce",
  50. "content-length": "0",
  51. "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",
  52. "cookie": "myCookie=myCookiee",
  53. "content-type": "multipart/form-data; boundary=--------------------------511827528311442099887851"
  54. },
  55. "json": null,
  56. "url": "https://postman-echo.com/post"
  57. Response body: }

答案1

得分: 1

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

  1. req.write(formData.getBuffer());

也不需要手动设置Content-Type标头。使用formData.getHeaders()

  1. headers: {
  2. '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',
  3. 'Cookie': 'myCookie=myCookiee',
  4. ...formData.getHeaders(),
  5. },
英文:

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

  1. req.write(formData.getBuffer());

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

  1. headers: {
  2. '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',
  3. 'Cookie': 'myCookie=myCookiee',
  4. ...formData.getHeaders(),
  5. },

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:

确定