Comments (25)
`URLSessionDataTaskDidReceiveData(session, dataTask, data) {
//console.log("URLSessionDataTaskDidReceiveData");
// we have a response in the data...
let jsonString = new NSString({ data: data, encoding: NSUTF8StringEncoding });
let json = JSON.parse(jsonString.toString())
// do what you want with your data
console.log(JSON.stringify(json))
}`
from nativescript-background-http.
@Ericky14
task.on("responded", (e) => { let res = JSON.parse(e.data) })
from nativescript-background-http.
The event provided to the "complete" callback contains the response:
task.on("complete", this.uploadComplete);
private uploadComplete(completeEvent) {
console.log(completeEvent.response.getBodyAsString());
}
from nativescript-background-http.
Is there any implementation for this as yet? I am uploading a file to parse server where the server returns a json object containing the url for the file which is needed by my app.
from nativescript-background-http.
How can I get the response from the server? That will be great
from nativescript-background-http.
First, thank you for this plugin. Second, I would also love to have the ability for a response message from the server to be passed back so that, if something goes wrong on the server, a reason for the error can be displayed to the end user.
In the meantime, can you tell me how the module's error event is currently being triggered? I thought that I could set the HTTP status code of the server script to be 404 or 422 and that would trigger bg-http's error event, but it does not seem to be working that way. Any help appreciated. Thanks!
from nativescript-background-http.
I have a similar situation as dondragon2. I cant currently utilize any data that my api sends back after a successful upload, like a photo id created in my photos table for example. Are there plans to implement this enhancement?
from nativescript-background-http.
THANK YOU @developper89 🌮🌮🌮🌮🌮🌮🌮🌮🌮🌮🌮🌮🌮🌮🌮🌮🌮🌮🌮🌮🌮
from nativescript-background-http.
FYI for all who find this thread, until @PanayotCankov releases an official enhancement, here's how you can get access to the data via the task in your models. This is only for iOS, mind you:
In background-http.ios.js:
URLSessionDataTaskDidReceiveData: function (session, dataTask, data) {
var jsTask = getTask(session, dataTask);
let jsonString = new NSString({ data: data, encoding: NSUTF8StringEncoding });
let json = JSON.parse(jsonString.toString());
jsTask.notify({ eventName: "responded", object: jsTask, data:json });
},
You can call the eventName whatever you want. I perform all my done uploading operations when that event is fired, for example:
task.on("progress", logEvent);
task.on("error", logEvent);
task.on("complete", logEvent);
task.on("responded", logEvent);
function logEvent(e) {
var rtn = {
"status": e.eventName
}
if (e.eventName == 'complete') {
console.log('upload completed');
} else if (e.eventName == 'error') {
if (callBack) callBack(rtn);
} else if (e.eventName == 'progress') {
if (updateProgressFn) {
updateProgressFn({uploaded: e.object.upload, total: e.object.totalUpload});
}
} else if (e.eventName == 'responded') {
rtn.data = e.data;
console.log('the server has responded with data.');
if (callBack) callBack(rtn);
}
}
from nativescript-background-http.
@davecoffin I found an issue with this code, basically since Tasks are stored as a WeakMap (line 187 of that file) I am losing the "task" sometimes but very often, so when I get my response back the task is not there anymore and what I get back is new Task instead, I had to get rid of the WeakMap, I am thinking about making a PullRequest and see if it gets approved by also using your approach along with my "fix"
from nativescript-background-http.
How does this work for android? Is there are solution or a workaround?
from nativescript-background-http.
I've added the server response to the complete event arguments and created a pull request
from nativescript-background-http.
how to read server response data?
from nativescript-background-http.
@AceTheFace Does that solution still work?
I tried using it and it doesn't seem to work for me.
This is the result:
from nativescript-background-http.
@developper89
Actually, I still get the same thing, just a null character.
Is there any way I can debug this or know what's going on? It doesn't show up on Network Requests when running tns debug
The complete event fired fine I think, no errors.
{"eventName":"complete","object":{"_observers":{"progress":[{}],"error":[{}],"complete":[{}],"responded":[{}]},"_session":{"_id":"image-upload"},"_id":"image-upload{1}","_description":"{ 'uploading': googlelogo_color_160x56dp.png }","_upload":10590,"_totalUpload":10590,"_status":"complete"},"response":{}}
from nativescript-background-http.
@Ericky14 It looks like you are tying into the complete event, the event that returns data is "responded".
from nativescript-background-http.
@davecoffin
I know, I put that excerpt just to show the complete event fires without any error.
This is what I did:
task.on('responded', e => {
console.log(JSON.parse(e.data));
});
But I believe it is most likely a problem with the back-end. The back-end works for uploading from web, but not from mobile with nativescript.
from nativescript-background-http.
@Ericky14 Your problem appears to be that you are logging a json object. You are probably getting the data just fine, but you cant console.log an object. Try this:
task.on('responded', e => {
console.dir(JSON.parse(e.data));
});
from nativescript-background-http.
@Ericky14 To your point about uploading from web vs nativescript, if you can interact with your API using Postman, you should be able to from your app. What authentication does your server use? To get set up I'd write a method in your backend to return a string with no authentication, just as a proof of concept. Once your app is communicating with your backend you know that your problem exists in your execution.
from nativescript-background-http.
I get errors unless I modify the code to be what @developper89 posted:
BackgroundUploadDelegate.prototype.URLSessionDataTaskDidReceiveData = function (session, dataTask, data) {
dispatch_async(main_queue, function () {
var jsTask = Task.getTask(session, dataTask);
var jsonString = new NSString({ data: data, encoding: NSUTF8StringEncoding });
jsTask.notify({ eventName: "responded", object: jsTask, data: JSON.parse(jsonString.toString()) });
});
};
from nativescript-background-http.
How to ready in case of error , it does not goes to responded. i want to read response
from nativescript-background-http.
same, I would like to get a response when I have an error? however at the moment it only shows the code which isn't that useful in accurately diagnosing what's wrong with the request I've sent.
from nativescript-background-http.
Same here, please reopen this issue.
Basically, the responded
event is not always triggered. In case of error, it is just skipped (Android).
from nativescript-background-http.
@mshanak @davecoffin @developper89 @peterbsmyth @friedolinfoerder I still have same issue for android in case of api failure, I wanted to take the error json response from server but reponded & complete events are not triggered, can anyone please get back me with the solution, thanks in advance
from nativescript-background-http.
@mshanak @davecoffin @developper89 @peterbsmyth @friedolinfoerder I have same issue, unable to handle the response coming from server with 400 status for andriod.
Here is my code
`public multipartUpload(url: string, token: string, bgSessionName: string,
params: any, androidNotificationTitle: string = 'Sync complete') {
const session = bgHttp.session(bgSessionName);
const name = 'test';
const description = `${name} (${++this.counter})`;
const request = {
url: url,
method: 'POST',
headers: {
Authorization: this.apiHelper.getAuthHeaderAsString(token),
'Content-Disposition': 'form-data; name=' + name + 'filename=' + name,
'Content-Type': 'application/octet-stream'
},
description: description,
androidAutoDeleteAfterUpload: false,
androidNotificationTitle: androidNotificationTitle,
androidAutoClearNotification: true
};
setTimeout(() => { // Timeout of 20 seconds. If the upload doesn't complete in 20 seconds. it will return a 408.
if (!this.finishedUploading) {
this.response.next({responseCode: 408, data: 'Multipart upload timed out'});
}
}, 20000);
const task = session.multipartUpload(params, request);
this.bindTask(task);
return this.response;
}
/**
* Bind the task to the events. This will create callbacks for every stage of the call.
* (responsed, progress, complete, error)
*
* To get the response code and data from the response, please subscribe on MultipartHandlerService.response
*
* @param task - bgHttp.Task
*/
private bindTask(task: bgHttp.Task): any {
task.on('progress', this.onEvent.bind(this));
task.on('error', this.onEvent.bind(this));
task.on('responded', this.onEvent.bind(this));
task.on('complete', this.onEvent.bind(this));
this.lastEvent = '';
}
private onEvent(e): Observable<boolean> {
if (this.lastEvent !== e.eventName) {
// suppress all repeating progress events and only show the first one
this.lastEvent = e.eventName;
} else {
return;
}
if ( e.eventName === 'complete') {
console.log('completehandler')
this.finishedUploading = true;
this.response.next({responseCode: e.responseCode, data: JSON.parse(this.data)});
}
if ( e.eventName === 'responded') {
console.log('respondhandler')
this.data = e.data;
console.log(e.data)
}
if ( e.eventName === 'error') {
console.log(`Multipart HTTP error.
ResponseCode: ${e.responseCode},
Response: ${e.response}`);
}
this.events.push({
eventTitle: e.eventName + ' ' + e.object.description,
eventData: JSON.stringify({
error: e.error ? e.error.toString() : e.error,
currentBytes: e.currentBytes,
totalBytes: e.totalBytes,
body: e.data,
responseCode: e.responseCode
})
});
}
}
`
Can anyone help me in fixing this where should I make my changes to handle the error response coming from server. Thanks
from nativescript-background-http.
Related Issues (20)
- IOS internal server error 502
- Ability to set timeout HOT 1
- Does this plugin work to upload images to S3 buckets? HOT 1
- Access Server Response After Upload Completed
- Nativescript 7 Error HOT 11
- Weird task event data HOT 2
- [IOS] Failed to issue sandbox HOT 1
- Use with nativescript-ssl-pinning
- Class constructor Observable cannot be invoked without 'new'. HOT 5
- Error on upload image, no response and respondeCode -1 HOT 6
- An uncaught Exception occurred on "pool-3-thread-1" thread. HOT 1
- An uncaught error on background image uploading HOT 8
- An uncaught Exception : Context.startForegroundService() HOT 1
- Image error after upload
- 503 error after upgrade to NS 7
- Targeting Android Version 31 - crashes with java.lang.reflect.InvocationTargetException HOT 12
- [Android] Unable to handle error response coming from server
- IOS upload - Payload too large
- illegalArgumentException: defaultNotificationChannel HOT 2
- Nativescript multipartUpload fails/crashes in android 31 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nativescript-background-http.