# How to handle negative scenarios 401, 400, 500?

**URL:** <https://forum.robotframework.org/t/how-to-handle-negative-scenarios-401-400-500/6617>\
**Category:** Robot Framework\
**Created:** [6 January 2024 22:14 UTC](https://forum.robotframework.org/t/how-to-handle-negative-scenarios-401-400-500/6617 "2024-01-06T22:14:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![nishans](https://avatars.discourse-cdn.com/v4/letter/n/a183cd/32.png) [@nishans](https://forum.robotframework.org/u/nishans)\
**Post date:** [6 January 2024 22:14 UTC](https://forum.robotframework.org/t/how-to-handle-negative-scenarios-401-400-500/6617/1 "2024-01-06T22:14:08Z")

</div>

I am trying to validate negative scenario for my REST api testing and after passing negative value my response status will come 401 but not able to access response data as robot framework will give: HTTPError: 401 Client Error: Unauthorized for url:

How to handle it and get the response body of rest api.

 ![Screenshot 2024-01-07 at 0.04.22](https://europe1.discourse-cdn.com/flex005/uploads/robotframework1/original/2X/1/15ff80b582d705b8e6304694ed9291f221834e96.png)

Appreciate, If you could help on this.

---

<div class="post-metadata">

**Author:** ![\_daryl](https://avatars.discourse-cdn.com/v4/letter/_/45deac/32.png) [@\_daryl](https://forum.robotframework.org/u/_daryl)\
**Post date:** [6 January 2024 22:26 UTC](https://forum.robotframework.org/t/how-to-handle-negative-scenarios-401-400-500/6617/2 "2024-01-06T22:26:14Z")

</div>

You could wrap in a try catch or use one of the builtin keywords “Run Keyword And….”

Built in keyword link:  
[https://robotframework.org/robotframework/latest/libraries/BuiltIn.html#Run%20Keyword%20And%20Expect%20Error](https://robotframework.org/robotframework/latest/libraries/BuiltIn.html#Run%20Keyword%20And%20Expect%20Error)

Pretty sure the expect error will return the message, so you could possibly make a keyword that handles all status codes to reduce code duplication.

EDIT\*  
Link for examples of try catch

> **[TRY / EXCEPT / FINALLY exception catching and handling in Robot Framework |...](https://robocorp.com/docs/languages-and-frameworks/robot-framework/try-except-finally-exception-catching-and-handling)**
>
> How to use TRY / EXCEPT / FINALLY to catch and handle errors in Robot Framework.

---

<div class="post-metadata">

**Author:** ![nishans](https://avatars.discourse-cdn.com/v4/letter/n/a183cd/32.png) [@nishans](https://forum.robotframework.org/u/nishans)\
**Post date:** [6 January 2024 23:12 UTC](https://forum.robotframework.org/t/how-to-handle-negative-scenarios-401-400-500/6617/3 "2024-01-06T23:12:45Z")

</div>

Thank you for your reply. I’m new to robot framework. can you correct me if i’m wrong? 🙂

# ==========================

Common Keywords.robot file

\*\*\* Keywords \*\*\*

Verify Error Message  
[Arguments] ${parsed\_response} ${expected\_status\_code}  
Error ‘${expected\_status\_code}’ == ‘401’  
… Should Contain ${parsed\_response[‘error\_message’]} Unauthorized # Verify the ‘error\_message’ field for 401  
… ELSE IF ‘${expected\_status\_code}’ == ‘404’  
… Should Contain ${parsed\_response[‘error\_message’]} Not Found # Verify the ‘error\_message’ field for 404  
… ELSE IF ‘${expected\_status\_code}’ == ‘500’  
… Should Contain ${parsed\_response[‘error\_message’]} Internal Server Error # Verify the ‘error\_message’ field for 500  
… ELSE  
… Log No specific error message verification for status code ${expected\_status\_code}

Calling\_API\_POST  
[Arguments] ${method} ${endpoint} ${expected\_status\_code} ${data} ${headers} ${timeout}  
${response}= Run Keyword RequestsLibrary.${method} ${endpoint} json=${data} headers=${headers} timeout=${TIMEOUT}  
${parsed\_response}= Set Variable ${response.json()}  
Should Be Equal As Strings ${response.status\_code} ${expected\_status\_code}  
Run Keyword If ${response.status\_code} != 200 Backend\_CommonKeywords.Verify Error Message ${parsed\_response} ${expected\_status\_code}

[Return] ${response}

# ==========================

testcase.robot file

\*\*\* Keywords \*\*\*

POST /api/auth/oauth/token  
[Tags] Login Dashboard Regression  
[Arguments] ${data} ${expected\_status\_code} ${parameter\_name} ${expected\_value} @{expected\_values}

Run Keyword If “${DATA\_NIC\_VERIFY}” != “None” Set To Dictionary ${data} request\_token=${DATA\_NIC\_VERIFY}  
${request\_headers}= Backend\_CommonKeywords.Onboarding\_Headers  
${response}= Backend\_CommonKeywords.Calling\_API\_POST POST ${BACKEND\_URL}/api/auth/oauth/token ${expected\_status\_code} ${data} ${request\_headers} ${TIMEOUT}

```
${length}= Get Length ${expected_values}

```

FOR ${index} IN RANGE 0 ${length} 2  
${expected\_param}= Set Variable ${expected\_values}[${index}]  
${expected\_value}= Set Variable ${expected\_values}[${index + 1}]  
Backend\_CommonKeywords.Validating\_Response\_Message ${response.content} ${expected\_param} ${expected\_value}  
END

\*\*\* Test Cases \*\*\*

POST /api/auth/oauth/token - Fail  
[Tags] Login Dashboard Regression  
[Template] POST /api/auth/oauth/token

${LOGIN\_ACCESS\_TOKEN\_INVALID\_PIN} 401

---

<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:** [7 January 2024 04:48 UTC](https://forum.robotframework.org/t/how-to-handle-negative-scenarios-401-400-500/6617/4 "2024-01-07T04:48:00Z")

</div>

Hi Nishan,

In the documentation fo [POST](https://marketsquare.github.io/robotframework-requests/doc/RequestsLibrary.html#POST) you’ll see `expected_status`

> By default this keyword fails if a status code with error values is returned in the response, this behavior can be modified using the `expected_status` and `msg` parameters, read more about it in [Status Should Be](https://marketsquare.github.io/robotframework-requests/doc/RequestsLibrary.html#Status%20Should%20Be) keyword documentation. In order to disable this implicit assert mechanism you can pass as `expected_status` the values `any` or `anything`.

> New requests keywords like [GET](https://marketsquare.github.io/robotframework-requests/doc/RequestsLibrary.html#GET) or [GET On Session](https://marketsquare.github.io/robotframework-requests/doc/RequestsLibrary.html#GET%20On%20Session) (starting from 0.8 version) already have an implicit assert mechanism that, by default, verifies the response status code. [Status Should Be](https://marketsquare.github.io/robotframework-requests/doc/RequestsLibrary.html#Status%20Should%20Be) keyword can be useful when you disable implicit assert using `expected_status=anything`.

You probably want something like this:

```python
POST ${endpoint} json=${data} expected_status=401 headers=${headers} timeout=${TIMEOUT}

```

Then that keyword will pass and the following keywords will work as you want.

You can do the same with `expected_status=400` and `expected_status=500` as needed

Dave.

---

<div class="post-metadata">

**Author:** ![nishans](https://avatars.discourse-cdn.com/v4/letter/n/a183cd/32.png) [@nishans](https://forum.robotframework.org/u/nishans)\
**Post date:** [7 January 2024 07:45 UTC](https://forum.robotframework.org/t/how-to-handle-negative-scenarios-401-400-500/6617/5 "2024-01-07T07:45:19Z")

</div>

> [@damies13](#):
>
> `expected_status=401 `

Thank you very much @_daryl . It’s working now.

\*\*\* Keywords \*\*\*  
Calling\_API\_POST  
[Arguments] ${method} ${endpoint} ${expected\_status\_code} ${data} ${headers} ${timeout}  
${response}= Run Keyword RequestsLibrary.${method} ${endpoint} json=${data} **expected\_status=** ${expected\_status\_code} headers=${headers} timeout=${TIMEOUT}  
${parsed\_response}= Set Variable ${response.json()}  
Should Be Equal As Strings ${response.status\_code} ${expected\_status\_code}

```
[Return] ${response}

```

 ![Screenshot 2024-01-07 at 9.41.15](https://europe1.discourse-cdn.com/flex005/uploads/robotframework1/original/2X/9/9e678667f1b29a94f79dff06995ecc710244f55b.png)

---

<div class="post-metadata">

**Author:** ![\_daryl](https://avatars.discourse-cdn.com/v4/letter/_/45deac/32.png) [@\_daryl](https://forum.robotframework.org/u/_daryl)\
**Post date:** [7 January 2024 10:37 UTC](https://forum.robotframework.org/t/how-to-handle-negative-scenarios-401-400-500/6617/6 "2024-01-07T10:37:21Z")

</div>

Was all @damies13 😉
