multipart/form-data post API可以从postman工作,但不能像预期的那样从react native工作

lrl1mhuk  于 2024-01-07  发布在  Postman
关注(0)|答案(1)|浏览(295)

我在react native中集成了一个API,在那里我遇到了一个解析错误。但是,从 Postman 那里,它工作得很好。
我尝试过通过uri,blob,base64字符串发送图像,但面临同样的问题。
下面是我的react-native代码:

  1. var data = new FormData();
  2. data.append("ImgInput", "/IMG_20161229_140335993.jpg");
  3. var xhr = new XMLHttpRequest();
  4. xhr.withCredentials = true;
  5. xhr.addEventListener("readystatechange", function () {
  6. if (this.readyState === 4) {
  7. console.log(this.responseText);
  8. }
  9. });
  10. xhr.open("POST", "http://212.48.69.65:7465/Core.svc/UploadImage?ApplicationName=www_spoiltpigrewards_com&SessionToken=7E2BE649-3136-4074-9B88-670D717C5828&[email protected]&StoreName=Cooperative&NumberOfProducts=2&Multishot=true&MultishotGuid=00000000-0000-0000-0000-000000000000&FileNumber=1&MaxFileNumber=2");
  11. xhr.setRequestHeader("Content-Type", "multipart/form-data");
  12. xhr.setRequestHeader("cache-control", "no-cache");
  13. xhr.send(data);

字符串
下面是json格式的postman集合。它工作正常。我希望从react-native代码中得到相同的输出。

  1. {
  2. "info": {
  3. "_postman_id": "7a0149d0-c96c-4431-8978-1b619968edf7",
  4. "name": "SpoiltPig",
  5. "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
  6. },
  7. "item": [
  8. {
  9. "name": "http://212.48.69.65:7465/Core.svc/UploadImage?ApplicationName=www_spoiltpigrewards_com&SessionToken=7E2BE649-3136-4074-9B88-670D717C5828&[email protected]&StoreName=Cooperative&NumberOfProducts=2&Multishot=true&MultishotGuid=00000000-0000-0000-0000-000000000000&FileNumber=1&MaxFileNumber=2",
  10. "request": {
  11. "method": "POST",
  12. "header": [
  13. ],
  14. "body": {
  15. "mode": "formdata",
  16. "formdata": [
  17. {
  18. "key": "ImgInput",
  19. "value": null,
  20. "type": "file"
  21. }
  22. ]
  23. },
  24. "url": {
  25. "raw": "http://212.48.69.65:7465/Core.svc/UploadImage?ApplicationName=www_spoiltpigrewards_com&SessionToken=7E2BE649-3136-4074-9B88-670D717C5828&[email protected]&StoreName=Cooperative&NumberOfProducts=2&Multishot=true&MultishotGuid=00000000-0000-0000-0000-000000000000&FileNumber=1&MaxFileNumber=2",
  26. "protocol": "http",
  27. "host": [
  28. "212",
  29. "48",
  30. "69",
  31. "65"
  32. ],
  33. "port": "7465",
  34. "path": [
  35. "Core.svc",
  36. "UploadImage"
  37. ],
  38. "query": [
  39. {
  40. "key": "ApplicationName",
  41. "value": "www_spoiltpigrewards_com"
  42. },
  43. {
  44. "key": "SessionToken",
  45. "value": "7E2BE649-3136-4074-9B88-670D717C5828"
  46. },
  47. {
  48. "key": "UserName",
  49. "value": "[email protected]"
  50. },
  51. {
  52. "key": "StoreName",
  53. "value": "Cooperative"
  54. },
  55. {
  56. "key": "NumberOfProducts",
  57. "value": "2"
  58. },
  59. {
  60. "key": "Multishot",
  61. "value": "true"
  62. },
  63. {
  64. "key": "MultishotGuid",
  65. "value": "00000000-0000-0000-0000-000000000000"
  66. },
  67. {
  68. "key": "FileNumber",
  69. "value": "1"
  70. },
  71. {
  72. "key": "MaxFileNumber",
  73. "value": "2"
  74. }
  75. ]
  76. }
  77. },
  78. "response": []
  79. }
  80. ]
  81. }

vc6uscn9

vc6uscn91#

在我的例子中,我循环遍历文件并将其添加到FormData中。这不会覆盖FormData中的文件。还从头文件中删除了"Content-Type"

  1. let formData = new FormData();
  2. data.getAll("imageField").forEach((file, index) => {
  3. formData.append("imageFiles", file);
  4. });

字符串

相关问题