formData PDF upload using XMLHttpRequest

Hello,

I’m trying to upload a PDF file to a web server as formData through XMLHttpRequest.
I get the file info using DocumentPicker from expo, which gives me:
{size: 20597, uri: “file:///data/user/0/host.exp.exponent/cache/Experi…entPicker/7cbee9cc-b5a7-4330-a047-a2c9bd778a44pdf”, name: “pdf-test.pdf”, type: “success”}

Now i’m trying to send the picked file via the following code:

var bodyFormData = new FormData();
    // bodyFormData.append('document[original_file]', file);
    bodyFormData.append('document[original_file]', {
      uri: uri,
      type: 'application/pdf',
      name: name,
    })
    console.debug("bodyFormData == ", bodyFormData)

    var xhr = new XMLHttpRequest();
    xhr.open('POST', CONSTANTS.STAGING_API.BASE_URL + CONSTANTS.STAGING_API.UPLOAD);
    xhr.setRequestHeader('Authorization', `${this.token}`)
    xhr.setRequestHeader('Accept', 'application/json')

    xhr.send(bodyFormData);

but using the rn_debugger i notice that the content-type property of my request is set to:
Content-Type: text/plain;charset=UTF-8
instead of application/pdf with required boundary setting…

Any idea how to fix this problem?

this is the API doc on how the request should look like:
Params:
Name | Type
document[original_file] | Multipart/Form-data

Headers:
Content-Type: multipart/form-data; boundary=----------XnJLe9ZIbbGUYtzPQJ16u1
Authorization: Bearer …
Host: example.org
Cookie:

Body:
------------XnJLe9ZIbbGUYtzPQJ16u1
Content-Disposition: form-data; name=“document[original_file]”; filename=“file.pdf”
Content-Type: application/pdf
Content-Length: 5858

[uploaded data]
------------XnJLe9ZIbbGUYtzPQJ16u1–

Thanx for any help!

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.