How to get API response using Restinstance with Robot Framework?

Viewed 2724

I'm a robot framework newbie. I'm trying everything I can possible think of but for some reason I'm still unable to get the correct result. I'm trying to use the combination of Rest and RestInstance library to create session for a website then log API's response.

When I use the following method, I got this error.

15:52:15.075 FAIL ValueError: need more than 1 value to unpack

Get User API Response
${auth}=    Create List    ${example_email}    ${pwd}
Create Session    a    website/api/sign_in    ${headers}    123    ${auth}

When I use this method I cannot successfully create an active session with account log-in thus return incorrect info.

${headers}=    Create Dictionary    csrf_token=123
Create Session    a    website/api/sign_in    ${headers}    123    ${auth}
Rest.Get    website/api/sign_in
Rest.Output    response

I'm not understanding what I did wrong. If anyone could point the right directions with be gratefully appreciated.

2 Answers

You can do the following You are trying to do the correct thing just assign Rest.Ouput to a variable and that way you can log it or verify the response

*** Keywords ***
Sample Rest
  ${Response_Body_Verify} =  set variable  Sample Data
  ${headers}=    Create Dictionary    csrf_token=123
  Create Session    a    website/api/sign_in    ${headers}    123    ${auth}
  Rest.Get    website/api/sign_in
  ${Response} =  Rest.Output    response body
  should be equal  ${Response}  ${Response_Body_Verify}

The problem looks like with the way you're calling the Create Session - what's the purpose of the "123" in the arguments?

Look at the library's documentation - the argument at that position is cookies, and it must be a dictionary.

So either create a dictionary and pass it there:

${cookies}=     Create Dictionary    var=123
Create Session    a    website/api/sign_in    ${headers}    ${cookies}    ${auth}

, or explicitly map the arguments you're passing to the keyword's paramters:

Create Session    a    website/api/sign_in    headers=${headers}    auth=${auth}

(I've dropped the "123" from the second example - I can't be sure what's its purpose, e.g. for which parameter is it)

Related