NativeScript / nativescript-background-http

Background Upload plugin for the NativeScript framework
Apache License 2.0
102 stars 50 forks source link

Besides success and failure, can other sever responses be received by this plugin? #101

Closed bigradish closed 6 years ago

bigradish commented 6 years ago

For example, the uploaded file's path on the server. Thanks.

lini commented 6 years ago

The server response for the upload request should be included in the responded event of the upload task. You can set the response body in your server code to include anything you want - for example, JSON data, as long as the response itself it's not too large. Then in the responded event you can access the response body:


task.on("responded", onRespondedEvent);
// ...
function onRespondedEvent(e) {
    console.log('server response is:' + e.data);
}
bigradish commented 6 years ago

@lini Thank you very much for your answers. I'll have a try. Another question: When I upload an image file, may I use "multipart/form-data" Content-Type? So far, I don't know how to receive the "application/octet-stream" file on my Golang server. Thank you again.

bigradish commented 6 years ago

@lini , I have known how to receive the "application/octet-stream" file on my Golang server.

lini commented 6 years ago

The sample app in this repository examples/SimpleBackgroundHttp shows how to upload in both ways:

https://github.com/NativeScript/nativescript-background-http/blob/aef1b7386b16707f402492f888723edd82ebfa24/examples/SimpleBackgroundHttp/app/main-page.ts#L75

bigradish commented 6 years ago

@lini Thanks for your answer. I used the multipart upload code in the sample, and tested on my android mobile phone, an error occurred, it showed the addParameter method can't be resolved (at line 150 of the background-http.android.js file). Please check this. Thank you.

lini commented 6 years ago

The name and value of the parameters you pass can only be of type string. If you are trying to use JSON or another type for the parameter value, you should convert it to string first.

bigradish commented 6 years ago

@lini Yes, exactly. Thank you so much. :)

bigradish commented 6 years ago

@lini I found that if the server response is a string, the e.data in the on 'responded' handler is a string of string, such as ""server response"". Is this a bug? I tested on android mobile phone.

lini commented 6 years ago

This is not normal behavior. You can test it using the demo server and demo app included in the plugin. It is going to return a normal string in the response - "Upload complete!" and not a double quoted string ""Upload complete!"".

I suggest you check your server code and verify what is written in the response object.

bigradish commented 6 years ago

@lini Thanks for your quick response. My server is normal. My server sends the response in JSON format. The e.data seems to keep the JSON data intact. So I use JSON.parse(e.data) to get the data, and then the double quoted string issue disappears. The data returned by the "responded" handler is not parsed even it is a JSON string, right?

lini commented 6 years ago

The data in the responded event is simply the body of the server response. There is no processing or JSON parsing done before it reaches you.