# RequestLibrary : HTTPError: For post api -400 Client Error: Bad Request for url

**URL:** <https://forum.robotframework.org/t/requestlibrary-httperror-for-post-api-400-client-error-bad-request-for-url/3627>\
**Category:** Robot Framework\
**Created:** [14 April 2022 17:03 UTC](https://forum.robotframework.org/t/requestlibrary-httperror-for-post-api-400-client-error-bad-request-for-url/3627 "2022-04-14T17:03:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bk-user](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/bk-user/32/1446_2.png) [@bk-user](https://forum.robotframework.org/u/bk-user)\
**Post date:** [14 April 2022 17:03 UTC](https://forum.robotframework.org/t/requestlibrary-httperror-for-post-api-400-client-error-bad-request-for-url/3627/1 "2022-04-14T17:03:51Z")

</div>

Hi all

I am using robot framework request library to automate post api.  
I have json payload and and some of value from that are need to pass dynamically.

I am able to successfully post request if I put static content in json file.

but as I want to pass dynamic content, I want to use dictionary. My code is as below

```
${header}= Create Dictionary Content-Type=application/json;charset=utf-8 Accept=application/json;charset=utf-8
# ${proxyDict}= Create Dictionary http=URL_proxy_http https=URL_proxy_https
    Create Session myssion ${api_url} headers=${header} disable_warnings=1
    ${file_data}= Get Binary File ../../${/}Resources${/}data.json
    ${body}= Create Dictionary customerName=rass status=false rejectionReason=${KWagrs} filingPeriod=04-2022 regionCode=33
    Log ${body}
    ${response}= Post On Session myssion /api/v1/create json=${body}

```

On execution I am getting below error- Anyone please help

 ![image](https://europe1.discourse-cdn.com/flex005/uploads/robotframework1/original/2X/e/e79a7d04b71e732ad4e037bd369bd661659413dd.png)

Below is working code with static content from json file -  
${header}= Create Dictionary Content-Type=application/json;charset=utf-8 Accept=application/json;charset=utf-8  
Create Session myssion ${api\_url} headers=${header} disable\_warnings=1  
${file\_data}= Get Binary File …/…/${/}Resources${/}data.json  
${body}= Create Dictionary customerName=rass status=false rejectionReason=${KWagrs} filingPeriod=04-2022 regionCode=33  
Log ${body}  
${response}= Post On Session myssion /api/v1/create data=${file\_data}

**Note** : I tried changing json= instead of data= and &{body}= instead ${body}

Suggest me any other way to pass dynamic content and correct json file?

---

<div class="post-metadata">

**Author:** ![damies13](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/damies13/32/623_2.png) [@damies13](https://forum.robotframework.org/u/damies13)\
**Post date:** [15 April 2022 00:44 UTC](https://forum.robotframework.org/t/requestlibrary-httperror-for-post-api-400-client-error-bad-request-for-url/3627/2 "2022-04-15T00:44:27Z")

</div>

Hi Bk-ava,

From looking at the error message returned by your app server, it looks like the solution might be as simple as changing this line:

> [@bk-user](#):
>
> ```python
> ${body}= Create Dictionary customerName=rass status=false rejectionReason=${KWagrs} filingPeriod=04-2022 regionCode=33
> 
> ```

to:

```python
&{body}= Create Dictionary customerName=rass status=False rejectionReason=${KWagrs} filingPeriod=04-2022 regionCode=33

```

Notice the & replacing $ in body variable and the capital F in `status=False`, the server is complaining you sent a text string ‘false’ when it was expecting a boolean which is why i’m making this suggestion.

If that doesn’t work, then can you add a line `Log ${file_data}` and show us the result of both Log for ${file\_data} and ${body} and hopefully we can spot the difference.

Another thing you can do is put exactly the same values in the ${body} dictionary as are in the static file then do something like this:

```python
Should Be Equal ${body} ${file_data}

```

or even try [Dictionaries Should Be Equal](http://robotframework.org/robotframework/latest/libraries/Collections.html#Dictionaries%20Should%20Be%20Equal)

```python
Dictionaries Should Be Equal ${body} ${file_data}

```

Hope that helps,

Dave.

---

<div class="post-metadata">

**Author:** ![bk-user](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/bk-user/32/1446_2.png) [@bk-user](https://forum.robotframework.org/u/bk-user)\
**Post date:** [15 April 2022 01:36 UTC](https://forum.robotframework.org/t/requestlibrary-httperror-for-post-api-400-client-error-bad-request-for-url/3627/3 "2022-04-15T01:36:02Z")

</div>

@damies13

I tried suggested ways–

1. modified status=false to status=False ----not working

2. Changed to & body -------not working

3. Printed both ${body} ${file\_data}

 ![image](https://europe1.discourse-cdn.com/flex005/uploads/robotframework1/original/2X/0/02cc9325bbe87d142a47b4f51f532fe77daa6c9f.png)

---

<div class="post-metadata">

**Author:** ![damies13](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/damies13/32/623_2.png) [@damies13](https://forum.robotframework.org/u/damies13)\
**Post date:** [16 April 2022 05:48 UTC](https://forum.robotframework.org/t/requestlibrary-httperror-for-post-api-400-client-error-bad-request-for-url/3627/4 "2022-04-16T05:48:01Z")

</div>

Hi Bk-ava,

I think your close, don’t worry with the should be equal or Dictionaries Should Be Equal as the file is read as a binary string so is not going to be equal unless we either json decode the string or json encode the dictionary.

try it like this:

```python
Create Session myssion ${api_url} headers=${header} disable_warnings=1
&{body}= Create Dictionary customerName=rass status=False rejectionReason=${KWagrs} filingPeriod=04-2022 regionCode=33
${response}= Post On Session myssion /api/v1/create json=${body}

```

The post on session needs to be json not data when sending a dictionary, it’s done as the document said it would when you passed a dictionary o data and urlencoded the values not json encoded them

If that still doesn’t work It also might be useful to do this:

```python
Create Session myssion ${api_url} headers=${header} disable_warnings=1
${file_data}= Get Binary File ../../${/}Resources${/}data.json
${response}= Post On Session myssion /api/v1/create data=${file_data}

&{body}= Create Dictionary customerName=rass status=False rejectionReason=${KWagrs} filingPeriod=04-2022 regionCode=33
${response}= Post On Session myssion /api/v1/create json=${body}

```

Then we can compare the difference between request bodies and headers on both Post On Session’s so we can see what’s different.

There are other options e.g.:

- construct the body as a string and then send that string as the post data (basically similar to what you are doing with the file)
- convert the dictionary to a json string, I think there was a library that did that, otherwise calling native python to do it
- then there is always [RESTinstance](https://asyrjasalo.github.io/RESTinstance/#library-documentation-top)

But I don’t think you’ll get there any quicker than trying to get your example above working, but if we don’t make any progress we can fall back to these.

Dave.

---

<div class="post-metadata">

**Author:** ![bk-user](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/bk-user/32/1446_2.png) [@bk-user](https://forum.robotframework.org/u/bk-user)\
**Post date:** [16 April 2022 06:29 UTC](https://forum.robotframework.org/t/requestlibrary-httperror-for-post-api-400-client-error-bad-request-for-url/3627/5 "2022-04-16T06:29:47Z")

</div>

@damies13  
Root cause was passing status=False treating that as string .  
Converted status to bool and passed then worked  
${status}= Convert To Boolean ${status}

Thank you

---

<div class="post-metadata">

**Author:** ![megha1](https://avatars.discourse-cdn.com/v4/letter/m/c2a13f/32.png) [@megha1](https://forum.robotframework.org/u/megha1)\
**Post date:** [27 February 2024 13:26 UTC](https://forum.robotframework.org/t/requestlibrary-httperror-for-post-api-400-client-error-bad-request-for-url/3627/6 "2024-02-27T13:26:43Z")

</div>

i was also stuck with the same issue , it resolved after adding ${true}= Convert To Boolean True
