2

I have a case in my application where I need to send data as form data to a server. The data includes a message and an optional list of files. The problem I'm facing is that when sending the request it's not being formed properly.

Request Payload

Expected (sample with the same request in the browser)

Expected Request Payload

Actual (resulting request when running in NativeScript) Actual Request Payload

The actual result is that the payload is somehow being URL encoded.

Code example

sendData({ id, message, files }) {
  const config = {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  };

  const payload = new FormData();
  payload.append('message', message);

  if (files && files.length > 0) {
    files.forEach((file) => {
      payload.append(`files`, file, file.name);
    });
  }

  return AXIOS_INSTANCE.post(
    `/api/save/${id}`,
    payload,
    config
  );
}

As you can see from the above, I'm using axios and also I'm trying to use FormData to format the data. From my research it seems that NativeScript used to not support binary data via XHR - however looking at this merge request on GitHub it looks like it's been fixed about a year ago.

So my suspicion is that I'm doing something wrong, maybe there's an alternative to using FormData, or else I shouldn't use axios for this particular request?

Version Numbers

  • nativescript 6.8.0
  • tns-android 6.5.3
  • tns-ios 6.5.3
Daniel Grima
  • 2,765
  • 7
  • 34
  • 58

1 Answers1

2

Nativescript's background-http supports multipart form data.

See below for how its configured to do multipart upload

var bghttp = require("nativescript-background-http");
var session = bghttp.session("image-upload");
var request = {
  url: url,
  method: "POST",
  headers: {
    "Content-Type": "application/octet-stream"
  },
  description: "Uploading "
};

var params = [
  { name: "test", value: "value" },
  { name: "fileToUpload", filename: file, mimeType: "image/jpeg" }
];
var task = session.multipartUpload(params, request);
William Juan
  • 1,395
  • 1
  • 7
  • 10
  • Great thanks - that worked perfectly. Honestly I did see some other posts mentioning this package but was trying to see if I can do without it.. but anyway it's working great :) – Daniel Grima Jan 27 '21 at 08:35