passing json data using keyword

50 views
Skip to the first unread message

Arpan Gandhi

unread,
30 Nov 2022, 11:06:2830/11/2022
to robotframework-users
I have created one custom keyword for post request where i want to pass json data but when i try to pass that data in test file its giving me error until i pass it in single line.

keyword;
I_Send_Post_Request:
    [Arguments]    ${endpoint}    ${body}
    ${body}=    create dictionary  Content-Type=application/json        
    ${header}=  create dictionary  Content-Type=application/json  
    ${content}
    REST.POST    ${endpoint} ${body} ${header}
    ${status_code}=    Integer    response status
    ${reason_}=    String   response reason
    ${response_body}=    Object    response body
    Set Test Variable    ${reason_}    ${reason_}
    Set Test Variable    ${status_code}    ${status_code}
    Set Test Variable    ${response_body}   ${response_body}
    Log To Console    ${status_code}
    Log To Console    ${reason_}
    Log To COnsole    ${response_body}
   
    [Return]    ${status_code}        ${response_body}

***test case***
put_info:
    when I_send_put_request:      api/users/2    {name: morpheus
                                                   job:zion resident}

-----------
in single line its working fine but giving 404 on my platform 

Geist Eng

unread,
8 Jan 2023, 18:22:1308/01/2023
to robotframework-users
Robot has the Requests library.  My usage is the json is a robot variable that is passed to the POST KW in this lib.

| | # Mark that POST content is json
| | ${headers}= | Create Dictionary | Content-Type=application/json
| |
......
| | ${sid}= | Generate Random String | 13 | [LETTERS][NUMBERS]
| | Create Custom Session | ${sid} | ${url} | ${auth} | timeout=${timeout}
| |
| | # Post API Set to path: ${api path} ${data}
| | ${status} | ${resp}= | Run Keyword And Ignore Error
| | ... | Post On Session | ${sid} | ${api path} | json=${data} | timeout=${DUT API TIMEOUT} | headers=${headers}

Reply all
Reply to author
Forward
0 new messages