1

Hello im trying to test an api with a body:

{
  "customerAttributes": [
    {
      "customerId": 0,
      "id": 0,
      "name": "string",
      "value": "string"
    }
  ],
  "emailAddress": "string",
  "firstName": "string",
  "id": 0,
  "lastName": "string",
  "registered": true
}

Since it has more than one object i dont know how to make it work. this is what i have til the moment :

*** Settings ***

Library  RequestsLibrary

*** Variables ***

${Base_URL}=     http://localhost:8082 /api/v1



    *** Test Cases ***
    TC_004_POST_Customer
    
        Create session  customer   ${Base_URL}
        ${body}=    create dictionary   customerId=100    id=100    name=test    value=0  emailAdress=blabla@gmailcom    firstName=algo    id=101    lastName=testt    registered=true
        ${header}=  create dictionary   Content-Type=application/json
        ${response}=    Post On Session    customer     /customer       data=${body}        headers=${header}
        log to console  ${response.status_code}
        log to console  ${response.content}

Can someone give me a help? thanks!

IKK
  • 31
  • 1
  • 4

2 Answers2

2

You should be able to do something like this:

${inner}=  Create Dictionary  customerId=100  id=100  name=test  value=0
${array}=  Create List  ${inner}
${body}=   Create Dictionary  customerAttributes=${array}  emailAdress=blabla@gmailcom  firstName=algo  id=101  lastName=testt  registered=True
Boefst
  • 31
  • 5
  • Dint work :( shoud i change somenthing else in response or ? – IKK May 18 '21 at 10:19
  • Also add Accept=application/json to your headers if you ecpect a json response. I don't know if it's just how you sent the code here, but you intendation is not correct, also there's a space in the Base_url. – Boefst May 18 '21 at 11:57
2

You have to use json argument in POST on Session keyword, like that:

    TC_004_POST_Customer
    
        Create session  customer   ${Base_URL}
        ${body}=    create dictionary   customerId=100    id=100    name=test    value=0  emailAdress=blabla@gmailcom    firstName=algo    id=101    lastName=testt    registered=true
        ${header}=  create dictionary   Content-Type=application/json
        ${response}=    Post On Session    customer     /customer       json=${body}        headers=${header}
        log to console  ${response.status_code}
        log to console  ${response.content}

This is the documentation POST On Session Keyword

  • Amazing! thanks now it did work :D just had to change the data to json like you sayd. Thanks – IKK May 19 '21 at 11:09