Test passes with 'post request' but not 'POST On Session'

Noob Alert

Hey there,
New to API automation using Robot FW.
Not sure if I’m missing something really silly, but I’ve been sitting at my screen for hours trying to figure this out with no help from Google.

I’ve written a script for a Login POST request using a youtube tutorial that looks something like this:

Login
create session mysession {BASE_URL} {body}= create dictionary userName=testuser password=Password panelCode=string
{header}= create dictionary Content-Type=application/json {response}= POST On Session mysession /api/authentication/Login
data={body} headers={header}

 log to console  ${response.status_code}
 log to console  ${response.content}

 ${status_code}=  convert to string  ${response.status_code}
 should be equal  ${status_code}  200

The test keeps failing with the following error against keyword POST On Session:
“errors”:{"":["'u' is an invalid start of a value. Path: | LineNumber: 0 | BytePositionInLine: 0."]}}

I have tried adding single & double quotes to the body’s key value pairs - still fails.
Here’s the kicker → If I change ‘POST On Session’ to ‘post request’, the test PASSES!!!
Obviously this isn’t good because the keyword is deprecated, but I’m not sure what on earth I’m doing wrong :exploding_head:

Would really appreciate some help so that I can move forward. Could anybody point me in the right direction (or improve my googling skills) PLEASE?

TIA
J

Hi Jasmine,

Welcome to the RF community.

Your formatting got a little messed up in your post, so my first tip is to use back ticks (`) to format your test cases so people can see where you have spaces, which leads me to my second tip

In Robot Framework Space are really important and you need at least 2 spaces or 1 tab between elements (keywords, arguments etc), I usually use a tab and if the tab doesn’t show enough whitespace I but a space in front of it which will usually trigger that, if you prefer spaces, I suggest using 4 spaces so it’s clear you can clearly see where you have only 1 space or more. Not enough spaces is one of the most common issues.

So assuming you had enough white space I formatted your login how it should look:

Login
    create session mysession {BASE_URL} 
    {body}=    create dictionary     userName=testuser    password=Password  
  panelCode=string
    {header}=    create dictionary    Content-Type=application/json 
    {response}=    POST On Session    mysession    /api/authentication/Login    data={body}    headers={header}

    log to console    ${response.status_code}
    log to console    ${response.content}

    ${status_code}=    convert to string    ${response.status_code}
    should be equal    ${status_code}    200

From the error message you got, the fist thing you should probably check you have enough space between mysession and /api/authentication/Login or between /api/authentication/Login and data={body} as this is the most likely place the problem is based on the error you got.

Hopefully that helps,

Dave.

Hi Dave,

Thanks so much for the feedback, but it’s definitely not the whitespace. I’m pedantic with the spacing and have tried 2 spaces as well as tabs. The formatting got messed up when I pasted the script into the topic dialogue/frame.

Again, the test passes when I use the deprecated ‘post request’, but fails when I use ‘POST On Session’ with the error returned as specified above. Doesn’t this suggest that the issue is with the keyword ‘POST On Session’?

TIA
J

Hi Jasmine,

I did a bit of checking and my test cases where I used RequestsLibrary were using the old Post Request, I last updated those test cases in Oct 2020 (These been no updates to that app for a while) and I see from here that the change to the newer “On Session” syntax happened in Release 0.8.0 so that’s why I hadn’t encountered this issue.

I guess the first thing to do is confirm which version of robotframework-requests you have installed:
pip show robotframework-requests

If it’s not 0.8.2 (latest stable) then upgrade to the latest version:
pip install -U robotframework-requests

As you only recently started using it, it’s likely you already have the latest version, so assuming you do.

Have a look at this issue 'put request' failed with octet-stream file · Issue #212 · MarketSquare/robotframework-requests · GitHub and see if that’s the same as your issue? If not maybe you need to log a new issue there.

As far as I can see you’ve followed the documentation correctly :+1:

Dave.

Hi Jasmine,

the 0.8 cleaned up lot of weird code the request with deprecated keyword might work but most probably is not doing what you expect the test should. So I rally suggest you to find what’s wrong.

First of all, as @damies13 already asked, we really need the exact code you are using so that I can see whether there are mistakes in the syntax.

Thank you and kind regards,
Luca

Hi Guys,

@damies13 Thanks so much for the feedback. I just recently installed RFW and on checking the version of robotframework-requests, I can confirm it is latest stable (0.8.2).

@lucagiove it is definitely not a syntactical error. If it were, wouldn’t the deprecated keyword fail as well? Using the deprecated keyword is not failing and the test is spitting out the results I need it to. Here is the code again:

*** Settings ***
Library RequestsLibrary
Library Collections
Library OperatingSystem

*** Variables ***
${BASE_URL} = http://srv-test:8004

*** Test Cases ***

Login

create session  mysession  ${BASE_URL}
${body}=  create dictionary  userName=testuserone  password=Password1  panelCode=string
${header}=  create dictionary  Content-Type=application/json
${response}=  POST On Sesssion  mysession  /api/authentication/Login  headers=${header}  data=${body}

log to console  ${response.status_code}
log to console  ${response.content}

${status_code}=  convert to string  ${response.status_code}
should be equal  ${status_code}  200

${res_body}=  convert to string  ${response.content}
should contain  ${res_body}  0

There are 2 spaces in this sample code. I updated the whole script with 4 spaces - got the same failure. Updated the whole script again to use tabs - got the same failure. This script is failing on
{response}= POST On Sesssion mysession /api/authentication/Login headers={header} data=${body}
Error: “errors”:{"":["‘u’ is an invalid start of a value. Path: | LineNumber: 0 | BytePositionInLine: 0."]}}

Kind regards
J

Ok, I think the problem is that you’re passing the dictionary as “data=” instead of using “json=” parameter.
Deprecated keywords automatically converted data to json according to headers but this prevents testing of payloads that doesn’t match the headers.

3 Likes

@lucagiove so do you think this resolve might work for ‘POST On Session’? I’m not really keen on using deprecated keywords, unless there’s a reason I could/should?

@lucagiove IT WORKED!!! :smile: :smile: :smile:
The test is passing using POST On Session now. I’m so happy! Thank you so much!

1 Like

I was only explaining why I changed the behavior with the new keywords, please use the * on session.

Happy to help

1 Like

can you please share the working solution for the above

Hi @prabhat2206 ,

Please re-read the thread, the solution to Jasmine’s problem is there
(hint the post is marked ✓ Solution)

Dave.