Home > database >  Adding Json data in a variable in ROBOT framework
Adding Json data in a variable in ROBOT framework

Time:09-17

I am starting to work on automating API requests using ROBOT framework and came across the below issue.

I have this JSON data

{
"password": "123456789",
"username": "[email protected]"
}

I want to put the above in a variable and generate a token as a response.

Previously I had used the below

${body}=  create dictionary       [email protected]     password=123456789

I got the below error response HTTPError: 415 Client Error: Unsupported Media Type for url:

Can someone help me on this please.

Updated Code

*** Settings *** 
Library String 
Library Collections 
Library RequestsLibrary 

*** Variables *** 
Credentials 
${prod_url} localhost:3000/api 

*** Test Cases *** 
User login and token generation.

Create Session demo ${prod_url} 

${body}= create dictionary [email protected] password=123456789 

${header}= create dictionary Content-Type=application/json 

${login}= post on session demo /login data=${body} headers=${header} 

log to console ${login.content} 

Above is full code. What I need is the username and password needs to be in JSON format because My API is accepting only those values..

CodePudding user response:

Instead of the data argument, try passing the dictionary to the json one:

${login}=  post on session    demo    /login    json=${body}  
  headers=${header} 

When you use the data, the library passes as-is the string representation of the object - and in python a str of a dict is {'password': '123456789' ,... - e.g. it uses single quotes, not double ones, which is an invalid json.
Passing the dict to the other argument, json, will make it convert to the proper format.

  • Related