Giter Site home page Giter Site logo

Comments (25)

developper89 avatar developper89 commented on June 19, 2024 5

`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.

developper89 avatar developper89 commented on June 19, 2024 3

@Ericky14
task.on("responded", (e) => { let res = JSON.parse(e.data) })

from nativescript-background-http.

AceTheFace avatar AceTheFace commented on June 19, 2024 2

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.

dondragon2 avatar dondragon2 commented on June 19, 2024 1

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.

neopablix avatar neopablix commented on June 19, 2024 1

How can I get the response from the server? That will be great

from nativescript-background-http.

ComputerTinker avatar ComputerTinker commented on June 19, 2024

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.

davecoffin avatar davecoffin commented on June 19, 2024

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.

davecoffin avatar davecoffin commented on June 19, 2024

THANK YOU @developper89 🌮🌮🌮🌮🌮🌮🌮🌮🌮🌮🌮🌮🌮🌮🌮🌮🌮🌮🌮🌮🌮

from nativescript-background-http.

davecoffin avatar davecoffin commented on June 19, 2024

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.

Daxito avatar Daxito commented on June 19, 2024

@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.

friedolinfoerder avatar friedolinfoerder commented on June 19, 2024

How does this work for android? Is there are solution or a workaround?

from nativescript-background-http.

friedolinfoerder avatar friedolinfoerder commented on June 19, 2024

I've added the server response to the complete event arguments and created a pull request

from nativescript-background-http.

mshanak avatar mshanak commented on June 19, 2024

how to read server response data?

from nativescript-background-http.

Ericky14 avatar Ericky14 commented on June 19, 2024

@AceTheFace Does that solution still work?
I tried using it and it doesn't seem to work for me.
This is the result:
screen shot 2017-05-17 at 10 26 32 pm

from nativescript-background-http.

Ericky14 avatar Ericky14 commented on June 19, 2024

@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.

davecoffin avatar davecoffin commented on June 19, 2024

@Ericky14 It looks like you are tying into the complete event, the event that returns data is "responded".

from nativescript-background-http.

Ericky14 avatar Ericky14 commented on June 19, 2024

@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.

davecoffin avatar davecoffin commented on June 19, 2024

@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.

davecoffin avatar davecoffin commented on June 19, 2024

@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.

peterbsmyth avatar peterbsmyth commented on June 19, 2024

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.

acharyaks90 avatar acharyaks90 commented on June 19, 2024

How to ready in case of error , it does not goes to responded. i want to read response

from nativescript-background-http.

ahuang4321 avatar ahuang4321 commented on June 19, 2024

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.

Jonarod avatar Jonarod commented on June 19, 2024

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.

Srikanthjava972 avatar Srikanthjava972 commented on June 19, 2024

@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.

Srikanthjava972 avatar Srikanthjava972 commented on June 19, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.