Put request with long body

Hello everyone

Im quite new using robot framework and im having a problem, with a post request that as a big body :

{
“address”: {
“addressLine1”: “string”,
“addressLine2”: “string”,
“addressLine3”: “string”,
“city”: “string”,
“companyName”: “string”,
“country”: {
“abbreviation”: “string”,
“name”: “string”
},
“firstName”: “string”,
“id”: 0,
“isBusiness”: true,
“isDefault”: true,
“isoCountryAlpha2”: {
“alpha2”: “string”,
“name”: “string”
},
“isoCountrySubdivision”: “string”,
“lastName”: “string”,
“phoneFax”: {
“id”: 0,
“isActive”: true,
“isDefault”: true,
“phoneNumber”: “string”
},
“phonePrimary”: {
“id”: 0,
“isActive”: true,
“isDefault”: true,
“phoneNumber”: “string”
},
“phoneSecondary”: {
“id”: 0,
“isActive”: true,
“isDefault”: true,
“phoneNumber”: “string”
},
“postalCode”: “string”,
“state”: {
“abbreviation”: “string”,
“name”: “string”
},
“stateProvinceRegion”: “string”
},
“addressName”: “string”,
“applicationContext”: {
“applicationName”: “string”,
“autowireCapableBeanFactory”: {},
“beanDefinitionCount”: 0,
“beanDefinitionNames”: [
“string”
],
“classLoader”: {
“defaultAssertionStatus”: true,
“parent”: {}
},
“displayName”: “string”,
“environment”: {
“activeProfiles”: [
“string”
],
“defaultProfiles”: [
“string”
]
},
“id”: “string”,
“parent”: {},
“parentBeanFactory”: {},
“startupDate”: 0
},
“archived”: {},
“id”: 0
}

Since the request is only to add Address i belive i dont have to change all the filds of the body but im still very confused how to make it work. Some people have pointed to me to use jsonpath library but i still not sure how it would work to select the path that i might need to change. Im ok with small bodys but this one is making go nuts. Someone could give me a hand? Thanks in advance

Hi Jonas,

Just to clarify:

  • which library are you using to test your application
  • how do you currently have this data setup in your robot file?

Also another thing, this looks like it’s an example JSON body provided by the developer? you might want to check with your developer which of these fields are mandatory and or what the JSON looks like when generated by the client application. As an example are “addressLine2” and “addressLine3” always sent?, if they are, are they a blank string?

As there are a few ways to tackle this problem knowing these things will let me give you a better answer for your case.

Dave.

Hello ! First of all thanks. Till the moment i’ve been only using RequestsLibrary but i belive for this one i will need to use json library or json validator. Not sure yet. Till the moment i havent anything in my robot file because i dont know how to make it work. im ok making requests with small bodys but when i have json files like this example i dont know how to make it work. i’ve been searching and for the info that i found i have to make somenthing like key values.

About the mandatory fields its also another questions. I dont have that info. Thats all the info i got from the swagger documentation, this is an free Api from Broadleaf Commerce - dont know if you might know it - its more like an exercise they gave me so i can get ready for future projects using Robot Framework.

In a different kind of request what i’ve done and it did work was that :

Create session customer ${Base_URL}
${inner}= Create Dictionary customerId=200 id=200 name=Bruno value=0
${array}= Create List ${inner}
${body}= Create Dictionary customerAttributes=${array} emailAddress=xxxx@gmail.com firstName=algo id=200 lastName=Oliveira registered=True

But i dont know how i make it work here with a body like that one… I dont know which fields are mandatory and if they are all mandatory - i belive not - how could i make it work using this key - values of JsonLibrary.

Thanks once again for the help

Im pretty sure that noone of this what im doing is right but i have no idea how to make this work :x

*** Settings ***
Library RequestsLibrary
Library JSONLibrary

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

*** Test Cases ***
TC_007_PUT_Customer
${json}= Load Json From File Tests/Swagger/address.json
${json_object}= Update Value To Json ${json} $.address.addressLine1 Ratchadapisek Road

${queryParams}=     Create Dictionary       customerId=200
Create session  customer   ${Base_URL}

${body}=  Create Dictionary     data=${json_object}
${header}=  Create dictionary      Accept=application/json
${response}=  PUT On Session    customer     /customer/address?     params=${queryParams}   json=${body}    headers=${header}
log to console  ${response.status_code}
log to console  ${response.content}

Hi Jonas,

Since you are using requests library you have a few options, you can either create the dictionary structure like you have already done (in your second post) or you can just construct a string and send that as the body.

Creating the dictionary structure is probably the better approach as it’s easier to update the element values.

In case you didn’t know anything between curly braces “{” and “}” in a json string is the equivalent of a dictionary and anything between square braces “[” and “]” in a json string is the equivalent of a list.
The json body you constructed in your second post would look like this:

{"customerAttributes":[{"customerId":200, "id":200, "name":"Bruno", "value":0
}], "emailAddress":"xxxx@gmail.com", "firstName":"algo", "id":200, "lastName":"Oliveira", "registered":true}

or formatted it would look like:

{
    "customerAttributes": [
        {
            "customerId": 200,
            "id": 200,
            "name": "Bruno",
            "value": 0
        }
    ],
    "emailAddress": "xxxx@gmail.com",
    "firstName": "algo",
    "id": 200,
    "lastName": "Oliveira",
    "registered": true
}

When I formatted your original JSON from your first post it looked like this:

{
    "address": {
        "addressLine1": "string",
        "addressLine2": "string",
        "addressLine3": "string",
        "city": "string",
        "companyName": "string",
        "country": {
            "abbreviation": "string",
            "name": "string"
        },
        "firstName": "string",
        "id": 0,
        "isBusiness": true,
        "isDefault": true,
        "isoCountryAlpha2": {
            "alpha2": "string",
            "name": "string"
        },
        "isoCountrySubdivision": "string",
        "lastName": "string",
        "phoneFax": {
            "id": 0,
            "isActive": true,
            "isDefault": true,
            "phoneNumber": "string"
        },
        "phonePrimary": {
            "id": 0,
            "isActive": true,
            "isDefault": true,
            "phoneNumber": "string"
        },
        "phoneSecondary": {
            "id": 0,
            "isActive": true,
            "isDefault": true,
            "phoneNumber": "string"
        },
        "postalCode": "string",
        "state": {
            "abbreviation": "string",
            "name": "string"
        },
        "stateProvinceRegion": "string"
    },
    "addressName": "string",
    "applicationContext": {
        "applicationName": "string",
        "autowireCapableBeanFactory": {},
        "beanDefinitionCount": 0,
        "beanDefinitionNames": [
            "string"
        ],
        "classLoader": {
            "defaultAssertionStatus": true,
            "parent": {}
        },
        "displayName": "string",
        "environment": {
            "activeProfiles": [
                "string"
            ],
            "defaultProfiles": [
                "string"
            ]
        },
        "id": "string",
        "parent": {},
        "parentBeanFactory": {},
        "startupDate": 0
    },
    "archived": {},
    "id": 0
}

so for the ${body} you would just have 5 entries like this:

${body}=    Create Dictionary     address=${address} addressName=string applicationContext=${applicationContext} archived=${archived} id=0

Then you just need to construct the dictionaries for ${address} and ${applicationContext}, and ${archived} would be an empty dictionary

I just saw your 3rd post, yes that’s a valid approach too, did it work?

Dave.

Hi Jonas,

If your PUT On Session failed from the example in your 3rd post, try changing json=${body} to data=${body}

The reason is Load Json From File returns a dictionary object

Another useful trick is to use the Log keyword in your test case as you are developing the test case e.g.

Log    ${json}
Log    ${json_object}

This way you can confirm what’s inside each variable, once you have the test case working nicely just remove the unnecessary Log steps.

Dave

Thanks once again Dave !

Its very conforting to see that there is people like you in this word always up to help others.

Unfortonattly it didnt work . Only the way i did on my second post creating a ${inner} e ${array} worked for me , but like i sayd before that was a small body and kinda easy to work with. For the other example i have no idea whats going on but nothing works :confused: even when i try to make the request on the Swagger i get a 500 error. I have no clue what im doing wrong … i’ve tryed what you sayd about changing the data to json also but it didnt worked aswell…

Im smashing this api request for like a week already and i got nowhere.
I’ll leave here a link of the API if there is anyone up to have a look… i’ve find it very usefull for a test but at the same time im not getting anywhere with this:

It’s my first time using robot on a api and i’ve found it very hard to understand to be honest… working direct with websites, xpaths and stuff was so easy but when i got that “test” i’ve learned that i still dosnt know anything about robot. I would love if someone could show me how i could make this work properly since im very noob and cant even make it work from the swagger. Hopefully its just the api that sucks and i might know somenthing. Or not.

Once again im very gratful for you help Dave! that was awesome from you. All the best !

Hi Jonas,

Since this is a learning exercise and not an application you need to test for your employer, then I suggest you start with something that has easier to understand documentation.

I usually recommend people start out with open cart when learning any automation tool, it’s an open source online shopping cart (e-commerce) application with excellent documentation a good web front end and an api that has examples with example data.

  • You can download a virtual machine image or docker image (which ever is easier for you) with open cart already setup from here so you don’t need to follow a complex install process
  • You can also try out the live demo here
  • As you can see here they actually use python requests examples in the documentation (python requests is the python library behind the requestslibrary in robot framework)

The reason I recommend open cart is it has just enough complexity to be a useful “real world” learning tool (the demo applications that come with many test tools are too simple and not realistic of real applications) but it’s simple enough and well documented enough to not discourage people, plus it actually is a real world application used by many online stores.

Don’t be discouraged, keep trying and you’ll get there. I found that Broadleaf/swagger documentation hard to follow too.

Robot framework is one of the easiest test automation tools, but web api’s are one of the hardest types of automation for people without a programming background, one of the best things about Robot framework is the forum community is quite friendly, much better than other tools i’ve used over the years.

Good luck on your learning journey.

Dave.

1 Like