Connect To REST API Basic Example

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):

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

Hi Brendan,

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

You could use the requestslibrary, that will work as it basically uses the python native equivelent to curl.

Though an easier option might be RESTinstance as it’s designed specifically for connecting to rest apis.

Dave.

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

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 so that should make using the requests library in Robot framework easier.

Dave.

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!

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

*** 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.

2 Likes

Thank you. Is this the correct request library GitHub - MarketSquare/robotframework-requests: Robot Framework keyword library wrapper for 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

I ran the test and got:

D

Hi Brendan,

Yes that’s the right library, at the top of the page install stable version:

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.

Hi Brendan,

I just noticed that your using Python 2.7, Python 2.x is no longer supported 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 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.

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

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.