# Connect To REST API Basic Example

**URL:** <https://forum.robotframework.org/t/connect-to-rest-api-basic-example/2416>\
**Category:** Robot Framework\
**Created:** [15 September 2021 13:15 UTC](https://forum.robotframework.org/t/connect-to-rest-api-basic-example/2416 "2021-09-15T13:15:30Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![resourceful-idiot](https://avatars.discourse-cdn.com/v4/letter/r/ba9def/32.png) [@resourceful-idiot](https://forum.robotframework.org/u/resourceful-idiot)\
**Post date:** [15 September 2021 13:15 UTC](https://forum.robotframework.org/t/connect-to-rest-api-basic-example/2416/1 "2021-09-15T13:15:30Z")

</div>

Hi, I want connect to a bug tracker and post RF test result.  
Currently I am doing this via the GUI but it can’t be efficient. I have never tried to connect using an API before so I am unsure programmatically how to start the RF script off using this API code e.g. If I just wanted to log in what parameters + any special libraries I need to connect:

I looked at the API doc for the application (Mantis):

```python
curl --location -g --request GET '{{url}}/api/rest/users/me' \
--header 'Authorization: {{mytoken}}'

```

```python
curl --location -g --request POST '{{url}}/api/rest/users/' \
--header 'Authorization: {{token}}' \
--header 'Content-Type: application/json' \
--data-raw '{
  "username": "testuser",
  "password": "p@ssw0rd",
}'

```

---

<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 September 2021 13:42 UTC](https://forum.robotframework.org/t/connect-to-rest-api-basic-example/2416/2 "2021-09-15T13:42:13Z")

</div>

Hi Brendan,

The first clue for how to approach this is `/api/rest`

You could use the [requestslibrary](https://robotframework-requests.netlify.app/doc/requestslibrary), that will work as it basically uses the python native equivelent to curl.

Though an easier option might be [RESTinstance](https://github.com/asyrjasalo/RESTinstance/) as it’s designed specifically for connecting to rest apis.

Dave.

---

<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 September 2021 14:03 UTC](https://forum.robotframework.org/t/connect-to-rest-api-basic-example/2416/3 "2021-09-15T14:03:21Z")

</div>

Hi Brendan,

From a quick look at the Mantis docs, it looks like the first thing you’ll need to do is [create a authentication token](https://www.testingdocs.com/create-api-token-in-mantisbt/)

I was hoping I could knock a quick example from the demo site, but not that easy.

I did also find in the docs that you can switch language to [python Requests](https://documenter.getpostman.com/view/29959/mantis-bug-tracker-rest-api/7Lt6zkP#intro) so that should make using the requests library in Robot framework easier.

Dave.

---

<div class="post-metadata">

**Author:** ![resourceful-idiot](https://avatars.discourse-cdn.com/v4/letter/r/ba9def/32.png) [@resourceful-idiot](https://forum.robotframework.org/u/resourceful-idiot)\
**Post date:** [15 September 2021 15:03 UTC](https://forum.robotframework.org/t/connect-to-rest-api-basic-example/2416/4 "2021-09-15T15:03:05Z")

</div>

Hi Dave. I’ve created an auth token.

I’ve looked about for examples of basic RF API/REST interaction but can’t seem to find anything that makes any sense!

---

<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 September 2021 15:45 UTC](https://forum.robotframework.org/t/connect-to-rest-api-basic-example/2416/5 "2021-09-15T15:45:47Z")

</div>

Ok I should be going to bed (it’s ~1:30am) I’ll try to build a simple example, based on documentation for RequestsLibrary and the python request example from the mantis docs

```python
***Settings***
	Library RequestsLibrary

***Variables***
${MantisToken} <put your token here>
${MantisServer} https://your.mantis.server

***Test Cases***
Get My User Info
	&{headers}= Create Dictionary Authorization=${MantisToken}
	Create Session	mantis ${MantisServer} headers=${headers}
	${response}=	GET On Session	mantis	/api/rest/users/me
	Log ${response}

```

Hopefully that will get you started,

Dave.

---

<div class="post-metadata">

**Author:** ![resourceful-idiot](https://avatars.discourse-cdn.com/v4/letter/r/ba9def/32.png) [@resourceful-idiot](https://forum.robotframework.org/u/resourceful-idiot)\
**Post date:** [16 September 2021 11:19 UTC](https://forum.robotframework.org/t/connect-to-rest-api-basic-example/2416/6 "2021-09-16T11:19:15Z")

</div>

> [@damies13](#):
>
> `Library RequestsLibrary`

Thank you. Is this the correct request library [GitHub - MarketSquare/robotframework-requests: Robot Framework keyword library wrapper for requests](https://github.com/MarketSquare/robotframework-requests)

I have to install it manually on an offline machine. I ra  
n from C:\Python27: python -m easy\_install robotframework-requests-master and got the following

![rf-req1a](https://europe1.discourse-cdn.com/flex005/uploads/robotframework1/original/1X/537917ce6a48744ac95b08304d4fbe585ec37b12.png)

I ran the test and got:

D

---

<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 September 2021 12:38 UTC](https://forum.robotframework.org/t/connect-to-rest-api-basic-example/2416/7 "2021-09-16T12:38:45Z")

</div>

Hi Brendan,

Yes that’s the right library, at the top of the page [install stable version](https://github.com/MarketSquare/robotframework-requests#install-stable-version):

```python
pip install robotframework-requests

```

you may need to use pip3 instead of pip, depend on how your machine is setup, that’s what I used when I installed it.

Dave.

---

<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 September 2021 13:02 UTC](https://forum.robotframework.org/t/connect-to-rest-api-basic-example/2416/8 "2021-09-16T13:02:00Z")

</div>

Hi Brendan,

I just noticed that your using Python 2.7, [Python 2.x is no longer supported](https://www.python.org/doc/sunset-python-2/) so you should upgrade to python 3.7+ (python 3.6 only has a few months left, so it would not be wise to use that version now)

Also you’ll need robotframework-requests version 0.8 or newer to use the example I gave you. I see you tried to install version 0.9.1 which is good, the only problem is that while robotframework-requests says it supports python 2+ the underlying [requests library](https://github.com/kennethreitz/requests) that robotframework-requests is relying on is not available for your version of python thats what the error is you got.

Also windows 2013? also that’s out of support for since 2018 too, you really are making life hard for yourself.

Dave.

---

<div class="post-metadata">

**Author:** ![resourceful-idiot](https://avatars.discourse-cdn.com/v4/letter/r/ba9def/32.png) [@resourceful-idiot](https://forum.robotframework.org/u/resourceful-idiot)\
**Post date:** [16 September 2021 15:18 UTC](https://forum.robotframework.org/t/connect-to-rest-api-basic-example/2416/9 "2021-09-16T15:18:28Z")

</div>

Whats the best way to update from 2.7, should I create a new folder e.g. Python310 for Python latest and do clean install of everything from scratch then uninstall 2.7

---

<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 September 2021 23:29 UTC](https://forum.robotframework.org/t/connect-to-rest-api-basic-example/2416/10 "2021-09-16T23:29:47Z")

</div>

Hi Brendan,

Usually python will install to a directory for each version, so python 3 can sit beside python 2 and even different versions of each.  
Once you install python 3.10 (or even before, doesn’t matter) do a `pip list` to see which robotframework packages you have installed (they will start with robotframework- ) this will give you an idea which packages you’ll need to install in python 3. For python 3 the pip install command is usually pip3 instead of pip. (easy\_install doesn’t seem to exist anymore, so use pip to install stuff)

Once that’s done, if you use an editor that runs your robot files for you, you might need to tell it to use the newer python, or simply remove python 2.7 from your path.

Hope that helps,

Dave.

---

<div class="post-metadata">

**Author:** ![Greenfelder](https://avatars.discourse-cdn.com/v4/letter/g/4da419/32.png) [@Greenfelder](https://forum.robotframework.org/u/Greenfelder)\
**Post date:** [9 November 2021 06:35 UTC](https://forum.robotframework.org/t/connect-to-rest-api-basic-example/2416/14 "2021-11-09T06:35:54Z")

</div>

Most APIs require an API key.The easiest way to start using an API is by finding an HTTP client online, like REST-Client, Postman, or Paw.The next best way to pull data from an API is by building a URL from existing API documentation.

[myccpay](https://www.myccpay.pro/)
