This is a migrated thread and some comments may be shown as answers.

Http Module Issue on iOS

5 Answers 50 Views
NativeScript Insiders
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
TJ
Top achievements
Rank 1
TJ asked on 05 Jan 2015, 06:23 PM
The docs say that the content property on the HttpRequestOptions accepts an object (https://github.com/NativeScript/docs/blob/master/ApiReference/http/HttpRequestOptions.md), but if I pass an object the app crashes, at least on iOS.

5 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 06 Jan 2015, 08:24 AM
Hi TJ,

You can use content property to post some data to a server. Here is an example from our tests:

JavaScript
http.request({
        url: "https://httpbin.org/post", method: "POST",
        headers: { "Content-Type": "application/x-www-form-urlencoded" },
        content: "MyVariableOne=ValueOne&MyVariableTwo=ValueTwo"
    }).then(function (response) {
        var result = response.content.toJSON();
        try  {
            TKUnit.assert(result["form"]["MyVariableOne"] === "ValueOne" && result["form"]["MyVariableTwo"] === "ValueTwo", "Content not sent/received properly!");
            done(null);
        } catch (err) {
            done(err);
        }
    }, function (e) {
        done(e);
    });

You can post string or even binary data (some image. Can you post more info about your case?

Regards,
Vlad
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
TJ
Top achievements
Rank 1
answered on 06 Jan 2015, 03:24 PM
Hey Vlad,

I was assuming if I passed an object for the content property that the method would automatically generate the HTTP encoded post body for me. So given your example I would assume that:

content: { MyVariableOne: "ValueOne", MyVariableTwo: "ValueTwo" }

...would work the same as passing the encoded string. This is how jQuery's AJAX methods work and how I think most developers will expect this module to work. It's certainly a lot cleaner. At the very least the docs are currently wrong, as they say that the content property expects an object, not a string (https://github.com/NativeScript/docs/blob/master/ApiReference/http/HttpRequestOptions.md).





0
Vlad
Telerik team
answered on 07 Jan 2015, 07:24 AM
Hi TJ,

Your suggestion makes perfect sense and we will add automatic conversion between JavaScript object and url encoded data for the next version similar to jQuery.param() method.

Thank you once again for your feedback!

Regards,
Vlad
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Vlad
Telerik team
answered on 07 Jan 2015, 02:38 PM
Hello TJ,

By the way you can post JSON currently with our HTTP client. Here is an example from our tests:
http.request({
        url: "https://httpbin.org/post", method: "POST",
        headers: { "Content-Type": "application/json" },
        content: JSON.stringify({ MyVariableOne: "ValueOne", MyVariableTwo: "ValueTwo" })
    }).then(function (response) {
        var result = response.content.toJSON();
        try  {
            TKUnit.assert(result["json"]["MyVariableOne"] === "ValueOne" && result["json"]["MyVariableTwo"] === "ValueTwo", "Content not sent/received properly!");
            done(null);
        } catch (err) {
            done(err);
        }
    }, function (e) {
        done(e);
    });

This can be more convenient since serializing plain JavaScript object/array to form data can be tricky.


Regards,
Vlad
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
TJ
Top achievements
Rank 1
answered on 07 Jan 2015, 02:52 PM
Ah, the JSON.stringify() approach is definitely a lot cleaner (https://github.com/tjvantoll/grocery-list/commit/91a1284077e9555a538077acaea7319014cc9a35).

Thanks!
Tags
NativeScript Insiders
Asked by
TJ
Top achievements
Rank 1
Answers by
Vlad
Telerik team
TJ
Top achievements
Rank 1
Share this question
or