2
votes

I am trying to upload an image to a server using POST and then expect a json string which I need to process in my NativeScript app.

I have tried using fetch but it require Blob/File web API that is not supported by NativeScript.

I have tried also using nativescript-background-http but it does not allow me to process the response (json string). Its oncomplete() event only pass me the task object which does not contain the response string.

I am thinking of extending the nativescript-background-http but it seems an overkill considering the basic nature of my requirement.

Question: How can I upload file in nativescript and process the response string as json?

I am new to nativescript so my apology if some of my observation above is wrong.

1

1 Answers

1
votes

Nativescript provides a http API which you want to use, and upload the file as FormData

Something like this:

http.request({
    url: "https://example.com",
    method: "POST",
    FormData: YOUR-FORM-DATA-HERE
})
.then(function (response) {
    console.log(response.content.toJSON());
});