Pass string argument as a part of request url?

3,248 views
Skip to first unread message

Zz Chen

unread,
Jul 1, 2013, 3:49:29 AM7/1/13
to robotframe...@googlegroups.com
Hi,
I would like to write a common function which composited by httplibrary.
The keyword as below:

Get Test Account
    [Arguments]  ${req name}
    Create Http Context  localhost  http
    GET  /account?name=${req name}


In my test, it seems not parse by RF ? It will send the raw string to server. Is that a problem i use the argument and string in GET request?
Andy idea about this problem?


BTW, I can come up a workaround by Catenate for this proposal.

Thanks!

Markus Bernhardt

unread,
Jul 1, 2013, 5:40:13 PM7/1/13
to chy...@gmail.com, robotframe...@googlegroups.com
Have you tried the keyword "Replace Variables"? Something like:

${url} Replace Variables /account?name=${req name}
GET ${url}

Kevin O.

unread,
Jul 2, 2013, 11:41:52 AM7/2/13
to robotframe...@googlegroups.com
If ${req name} contains spaces, ampersands, unicode characters etc., then you must encode it. Python has a method for that and I use a user keyword to call it. Here's a snippet:

Create Http Context  localhost  http
${param dict}=    Create Dictionary    name    ${req name}
${params}= Get Encoded Query Parameters ${param dict}
${response}= GET    /account?${params}

Get Encoded Query Parameters
    [Arguments]    ${query}    ${doseq}=${False}
    [Documentation]    Convert a mapping object or a sequence of two-element tuples to a “percent-encoded” string, suitable to pass to urlopen() above as the optional data argument. This is useful to pass a dictionary of form fields to a POST request. The resulting string is a series of key=value pairs separated by '&' characters, where both key and value are quoted using quote_plus() above. When a sequence of two-element tuples is used as the query argument, the first element of each tuple is a key and the second is a value. The value element in itself can be a sequence and in that case, if the optional parameter doseq is evaluates to True, individual key=value pairs separated by '&' are generated for each element of the value sequence for the key. The order of parameters in the encoded string will match the order of parameter tuples in the sequence. The urlparse module provides the functions parse_qs() and parse_qsl() which are used to parse query strings into Python data structures.
    ${urllib}=    Evaluate    urllib    urllib
    ${params}=    Call Method    ${urllib}    urlencode    ${query}
    [Return]    ${params}

Andrea Bisello

unread,
Jul 11, 2016, 3:13:06 AM7/11/16
to robotframework-users
now i suggest to use robotframework-requests and use payload in order to pass query param

for example


${queryParams}=     Create Dictionary       v=5

Create Session  sess2  http://domain.com
${resp}=  Get sess2  /api/test.php                 headers=${headers}        params=${queryParams}

will asks for http//domain.com/api/test.php?v=5
Reply all
Reply to author
Forward
0 new messages