locked
Brackets in Query field names causes exception RRS feed

  • Question

  • I'm building a client to talk to a REST API build around the Symfony2 framework.  As a standard for the framework form field names use a format of "formName[fieldName]"

    So a post request might look like this:

    "/app/checkCred?user[username]=Frank&user[accessToken]=1asdb71823"

    If I try to pass something like that into client.request it throws an exception: pplx::invalid_operation, at ln:983 in http_msg.h

    _m_impl->set_request_uri(uri(uri::encode_uri(path_query_fragment)));

    If I remove a the brackets, life is good.  I've tried the URI builder, and when I called builder.to_encoded_string() it executes fine and doesn't throw an exception for the URI being invalid, only when I try to pass the string to generate a request.  Considering this isn't actually a malformed uri, is there a way to work around this?

    Monday, January 7, 2013 9:07 PM

Answers

  • Hi Vapes,

    The problem you encountered is definitely caused by the encode_uri function, which seems not working properly.

    Directly encoding a given URI string by calling uri::encode_uri is hard, since each component of URI has different encoding policy, and by default the encode_uri function only take a least aggressive encoding strategy, which might miss some characters that should be encoded.

    There are 2 solutions for you :

    1. you can encode the query separately by invoking the uri::encode_component function, and concat the results together to construct an encoded uri string.

    2. Construct uri by using uri_builder (our recommended way). Since the uri_builder has the knowledge of each part of the uri, it will encode the uri correctly (As you already experimented.)

    Of course, we will try to improve the usability of uri encoding functions, and thanks for your feedback.


    Hong

    • Marked as answer by Vapes Friday, January 11, 2013 5:39 PM
    Wednesday, January 9, 2013 4:01 AM

All replies

  • Hi Vapes,

    The problem you encountered is definitely caused by the encode_uri function, which seems not working properly.

    Directly encoding a given URI string by calling uri::encode_uri is hard, since each component of URI has different encoding policy, and by default the encode_uri function only take a least aggressive encoding strategy, which might miss some characters that should be encoded.

    There are 2 solutions for you :

    1. you can encode the query separately by invoking the uri::encode_component function, and concat the results together to construct an encoded uri string.

    2. Construct uri by using uri_builder (our recommended way). Since the uri_builder has the knowledge of each part of the uri, it will encode the uri correctly (As you already experimented.)

    Of course, we will try to improve the usability of uri encoding functions, and thanks for your feedback.


    Hong

    • Marked as answer by Vapes Friday, January 11, 2013 5:39 PM
    Wednesday, January 9, 2013 4:01 AM
  • Hi Hong,

    Thanks for the response.  I hadn't realized I could build my own request and send it to client.request(), so using the uri_builder did the trick.  Ultimately I found that for my use case I needed to pack the data in the body and send it as a form, but your information will help when I have to make similar requests with GET.  

    Thanks!

    Thursday, January 10, 2013 8:59 PM