RequestLibrary : Post On session vs Post request

Hello,
I have some issues with the new requestlibrary version (0.9.1).

Here is my former code (with the 0.7.1 version) which works fine :

${header}= Create Dictionary Content-Type=application/json;charset=utf-8 Accept=application/json;charset=utf-8
Create Session mysession ${base_url} proxies=${proxy} headers=${header}
${body}= Get File MyFile.json
${response}= Post Request mysession myURI data=${body}

With the new version (0.9.1), I only modify the keyword :

${header}= Create Dictionary Content-Type=application/json;charset=utf-8 Accept=application/json;charset=utf-8
Create Session mysession ${base_url} proxies=${proxy} headers=${header}
${body}= Get File MyFile.json
${response}= Post On Session mysession myURI data=${body}

The JSON response is “BAD REQUEST” and I don’t know why… An idea ?

Thanks a lot !

When moving some code of mine over to latest I did notice that before I could use the ‘data’ field for raw data and json data.

Could you try using json= rather than data= in your Post On Session call?

Hi Brandon,
Thanks for your answer.
I’ve tried to replace “data” by “json” but it’s the same results…

Hmm well if you are familiar with using python you could do what I did here using utils in RequestsLibrary:

This allowed me to use the same data I had, convert it, and then send it in as data.

Unfortunatly, i’m not a Python developer. I use RF by using keyword only…
An another way to correct this problem is existing ? I hope I’m not the only one who have this kind of problem lol.

To do this in only robotframework I believe it would look like this:

*** Settings ***
Library    RequestsLibrary.utils

*** Test Cases ***
Some Test
    ${formatted_data}=      Format Data According To Header    session    data    headers

I had a similar problem and I already had a keyword that implemented the post request. Then, I created a dictionary of arguments with the keyword Create Arguments Dictionary and called it in such keyword and passed the created dictionary to post on session.

In such dictionary you will find the jason, data and everything that can be sent to a request.

I Send a Post Request
    [Arguments]    ${alias}    ${uri}    ${data}=${EMPTY}     ${json}=${EMPTY}    
    ...    ${params}=${EMPTY}     ${headers}=${EMPTY}     ${files}=${EMPTY}  
    ...    ${allow_redirects}=${EMPTY}    ${timeout}=${EMPTY}
    ...    ${kwargs}    Create Arguments Dictionary    data=${data}     json=${json}    
    ...    params=${params}     headers=${headers}     files=${files}     
    ...     allow_redirects=${allow_redirects}    timeout=${timeout}
    ${response}=    Post On Session     ${alias}    ${uri}    expected_status=any    &{kwargs}
    Set Test Variable    ${response}

Create Arguments Dictionary
    [Arguments]    ${data}=${EMPTY}     ${json}=${EMPTY}    ${params}=${EMPTY}     ${headers}=${EMPTY}     ${files}=${EMPTY}     ${allow_redirects}=${EMPTY}    ${timeout}=${EMPTY}
    ${kwargs}    Create Dictionary
    ${hasValue}    Run Keyword and return status     Should not be empty    ${headers}
    Run Keyword if    ${hasValue}    Set To Dictionary    ${kwargs}    headers=${headers}
    ${hasValue}    Run Keyword and return status     Should not be empty    ${timeout}
    Run Keyword if    ${hasValue}    Set To Dictionary    ${kwargs}    timeout=${timeout}
    ${hasValue}    Run Keyword and return status     Should not be empty    ${data}
    Run Keyword if    ${hasValue}    Set To Dictionary    ${kwargs}    data=${data}
    ${hasValue}    Run Keyword and return status     Should not be empty    ${params}
    Run Keyword if    ${hasValue}    Set To Dictionary    ${kwargs}    params=${params}
    ${hasValue}    Run Keyword and return status     Should not be empty    ${files}
    Run Keyword if    ${hasValue}    Set To Dictionary    ${kwargs}    files=${files}
    ${hasValue}    Run Keyword and return status     Should not be empty    ${allow_redirects}
    Run Keyword if    ${hasValue}    Set To Dictionary    ${kwargs}    allow_redirects=${allow_redirects}
   ${hasValue}    Run Keyword and return status     Should not be empty    ${json}
 Run Keyword if    ${hasValue}    Set To Dictionary    ${kwargs}    json=${json}
[Return]    ${kwargs}

I suggest to have a look on the server logs whether you get more information about the BAD REQUEST.
I think you can remove most of the headers part and pass json=.
Another point might be to properly convert the json file to a dictionary or something parsable from requests.

@Centaure13 let us know whether you resolved.

Thanks !
I will check all solutions and see the results !

did you allready try these solution:
change your:
${body} to &{body} ,
after that:
${response}= Post On Session mysession myURI json=${body}.

Hello all,

Here is the solution I’ve found :

${header}=    Create Dictionary    Content-Type=application/json;charset=utf-8    Accept=application/json;charset=utf-8
${proxyDict}=    Create Dictionary    http=URL_proxy_http    https=URL_proxy_https
Create Session    mysession    ${base_url}    headers=${header}    proxies=${proxyDict}
${body}=    Load JSON From File   Myfile.json    
${response}=    Post On Session    mysession    myURI    json=${body}  

Thanks for your help !