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

lrl1mhuk  于 12个月前  发布在  Postman
关注(0)|答案(1)|浏览(193)

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

var data = new FormData();
  data.append("ImgInput", "/IMG_20161229_140335993.jpg");

  var xhr = new XMLHttpRequest();
  xhr.withCredentials = true;

  xhr.addEventListener("readystatechange", function () {
    if (this.readyState === 4) {
      console.log(this.responseText);
    }
  });

  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");
  xhr.setRequestHeader("Content-Type", "multipart/form-data");
  xhr.setRequestHeader("cache-control", "no-cache");

  xhr.send(data);

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

{
    "info": {
        "_postman_id": "7a0149d0-c96c-4431-8978-1b619968edf7",
        "name": "SpoiltPig",
        "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
    },
    "item": [
        {
            "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",
            "request": {
                "method": "POST",
                "header": [
                ],
                "body": {
                    "mode": "formdata",
                    "formdata": [
                        {
                            "key": "ImgInput",
                            "value": null,
                            "type": "file"
                        }
                    ]
                },
                "url": {
                    "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",
                    "protocol": "http",
                    "host": [
                        "212",
                        "48",
                        "69",
                        "65"
                    ],
                    "port": "7465",
                    "path": [
                        "Core.svc",
                        "UploadImage"
                    ],
                    "query": [
                        {
                            "key": "ApplicationName",
                            "value": "www_spoiltpigrewards_com"
                        },
                        {
                            "key": "SessionToken",
                            "value": "7E2BE649-3136-4074-9B88-670D717C5828"
                        },
                        {
                            "key": "UserName",
                            "value": "[email protected]"
                        },
                        {
                            "key": "StoreName",
                            "value": "Cooperative"
                        },
                        {
                            "key": "NumberOfProducts",
                            "value": "2"
                        },
                        {
                            "key": "Multishot",
                            "value": "true"
                        },
                        {
                            "key": "MultishotGuid",
                            "value": "00000000-0000-0000-0000-000000000000"
                        },
                        {
                            "key": "FileNumber",
                            "value": "1"
                        },
                        {
                            "key": "MaxFileNumber",
                            "value": "2"
                        }
                    ]
                }
            },
            "response": []
        }
    ]
}

vc6uscn9

vc6uscn91#

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

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

字符串

相关问题