NativeScript / nativescript-background-http

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

Can I get the HTTP Response headers? #111

Closed geoffbullen closed 6 years ago

geoffbullen commented 6 years ago

Is it possible to get the response header parameters from nativescript-background-http.

session.uploadFile(filePath, request);

The API I'm calling uses a header parameter to give me the URL from which I can actually get the result of the file upload. So, I can't manage without it... I came across this https://github.com/NativeScript/nativescript-background-http/issues/16 but was left a bit unclear about whether this is going to be possible.

I need to get at response.headers['operation-location'] in order to check the result. It is not an API I own, so don't have any flexibility to change how it returns...

Does anyone know whether I can achieve what I want? Or not...

lini commented 6 years ago

I can give you an example for Android. The complete event contains a ServerResponse object that has the headers you need. Getting to them is a bit tricky, because it is a native object but here is some sample code:

task = this.session.uploadFile(this.file, request);
task.on("complete", onEvent);
function onEvent(e) {
    var headers = e.response.getHeaders();
    var iterator = headers.entrySet().iterator();
    while (iterator.hasNext()) {
        var header = iterator.next();
        console.log(header.getKey() + ": " + header.getValue());
    }
}

On iOS, there is a similar way to get the response headers, again from the complete event. In the onEvent code above the headers can be accessed from e.object.ios.response.allHeaderFields.

geoffbullen commented 6 years ago

Thankyou!! I would have taken a month to figure that out for myself...

Here is the code in iOS in case anyone is in the same boat...

let myDict:NSMutableDictionary<string,any> = e.object.ios.response.allHeaderFields; myDict.enumerateKeysAndObjectsUsingBlock(function(k, v) { console.log("k: " + k + ", v: " + v); })