Http Module Issue on iOS

6 posts, 0 answers
  1. TJ
    TJ avatar
    58 posts
    Member since:
    Aug 2013

    Posted 05 Jan 2015 Link to this post

    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.
  2. Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 06 Jan 2015 Link to this post

    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.

     
  3. TJ
    TJ avatar
    58 posts
    Member since:
    Aug 2013

    Posted 06 Jan 2015 in reply to Vlad Link to this post

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





  4. Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 07 Jan 2015 Link to this post

    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.

     
  5. Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 07 Jan 2015 Link to this post

    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.

     
  6. TJ
    TJ avatar
    58 posts
    Member since:
    Aug 2013

    Posted 07 Jan 2015 in reply to Vlad Link to this post

    Ah, the JSON.stringify() approach is definitely a lot cleaner (https://github.com/tjvantoll/grocery-list/commit/91a1284077e9555a538077acaea7319014cc9a35).

    Thanks!
Back to Top