API testing using RequestsLibrary.

300 views
Skip to first unread message

Sudhir kumar

unread,
Nov 9, 2017, 5:19:26 AM11/9/17
to robotframework-users
 
Hi all... i am using robot frame work for API testing using RequestsLibrary.. but i am facing below issue:
1. when comparing status code the argument type is not matching for ${resp.status_code} with 200  i.e: 

    Should Be Equal As Strings ${resp.status_code}, 200
    Argument types are: <type 'int'> <type 'unicode'>

2. While i am doing POST request i am getting status code : 415

     RequestsLibrary . Post Request URLP, /tsws/api/v1.0/messages/, ${header}

Documentation:

Send a POST request on the session object found using the

 Start / End / Elapsed: 20171109 15:34:15.623 / 20171109 15:34:15.679 / 00:00:00.056
 15:34:15.678 INFO Post Request using : alias=URLP, uri=/tsws/api/v1.0/messages/, data=<empty>, headers=None, files=None, allow_redirects=True
 15:34:15.679 INFO (u'/tsws/api/v1.0/messages/', 'content-type=application%2Fjson&messagetype=AGENT_HANDSHAKE', None, None, None)
 15:34:15.679 INFO ${resp} = <Response [415]>

please help ...!!!!!!

Zeff Morgan

unread,
Nov 9, 2017, 5:32:08 PM11/9/17
to robotframework-users

Sudhir,

What are you actually getting in your status_code property of the response object? Have you tried logging it to see what it looks like? Throw a 'Log    ${resp.status_code}' in there and take a look. Start with the most basic components and work from there.

I have to ask though, what file format are you using for your test files? I'm not accustomed to seeing commas. Maybe remove the comma in your assertion? It should probably read:

Should Be Equal As Strings    ${resp.status_code}    200

No commas.

Zeff

Sudhir kumar

unread,
Nov 13, 2017, 5:47:48 AM11/13/17
to robotframework-users
Thanks for the reply zeff ... but that comman is in report file not in script..

I have one more problem... can anyone tell me how to handle cookie in automation for API testing with "requests" library.?????

Thanks,
Sudhir  

Zeff Morgan

unread,
Nov 13, 2017, 11:12:52 AM11/13/17
to robotframework-users
Sudhir,

Did you try logging out ${resp.status_code}? If so, what do you get? I'm wondering if there's a bug in the API to have such a simple assertion fail. But without seeing your code, we're shooting in the dark, so seeing that would be helpful as well.

As to handling cookies, Bulkan's RequestsLibrary documentation doesn't call them out, but if you look at the source code, you see you can set simple cookies when you create your session. Here's an example of what you can do with cookies in RF-RequestsLibrary:

*** Settings ***
Library          RequestsLibrary

*** Test Cases ***
Get Cookies
    Create Session    google               http://www.google.com
    ${resp}=          Get Request          google                   /
    ${cookies}=       Create List          @{resp.cookies}
    Log               ${cookies}

Set Cookies
    ${cookies}=       Create Dictionary    cookie_id                 cookie value
    Create Session    httpbin              https://httpbin.org       cookies=${cookies}    debug=1    disable_warnings=1
    ${resp}=          Get Request          httpbin                   /cookies
    Log               ${resp.text}

It does appear that there are some more complex cookie scenarios that Python Requests actually handles from their documentation using a function that the RF RequestsLibrary doesn't expose, but it seems you should be able to do most of what you want by working with the RequestsCookieJar as if it were a dictionary.

HTH.

Zeff

Kevin O.

unread,
Nov 13, 2017, 11:49:45 AM11/13/17
to robotframework-users
When troubleshooting RequestsLibrary, I suggest:
* use loglevel debug when running the test(s)
* set debug=1 when calling Create Session - will show request headers and body in the log

You are sending data without headers. Also the data you are sending is in a variable called ${header}, which does not seem right.
415 means unsupported media type, which is probably because you are not sending any headers.

Regarding cookies - they are handled automatically by RequestsLibrary. If you need to use cookies set in one session in a different session, though, it takes some effort.

Zeff Morgan

unread,
Nov 13, 2017, 2:43:40 PM11/13/17
to robotframe...@googlegroups.com
Der. Thanks for pointing out the response code, Kevin. And +1 for setting debug=1 in the session. I was making a big assumption ... 

Sudhir, I was assuming you knew how to build a request for your custom API to begin with. What does your API require re: headers? And you're using a POST request, so I'm assuming you'd be intending to pass some kind of data. Based on the error message, I'm guessing you want to set your content-type and pass that in as your header argument. Then maybe messagetype=AGENT_HANDSHAKE is the payload you're trying to pass based on your content-type? If so, that belongs in your 'data' argument. Create a dictionary with content type and another with messagetype and pass those DICT's in your post request ...

${resp}=    Post Request    you_session_alias    uri    headers=${your_header_dictionary}    data=${your_data_dictionary}

You may want to just spend some time with Postman and your API until you understand what your API needs.

If you're going to be doing a lot of that for many different endpoints, I like creating a common object to deal with paths, creds, etc., and use yaml to store that variable data. Search for yaml in the User guide.

For future reference, if you post your code, this could have all been answered very quickly.

HTH

Zeff

On Thursday, November 9, 2017 at 5:19:26 AM UTC-5, Sudhir kumar wrote:

bagya ss

unread,
Dec 7, 2017, 9:38:12 AM12/7/17
to robotframe...@googlegroups.com

Hi,

 

I am also stuck with the Post request keyword for testing REST API. What is the correct way to pass the raw body content to the post request?

request raw body :

{

"username": "",

  "password": "",

  "mobile_id": ""

}

 I tried in 2 ways.

1.    Using Get File – passing the .json file

2.    Using Create Dictionary – passing the body as key value pairs(see the screenshot below)


 

And I get the following Error :


Starting test: Robot.REST TEST.swaggerpost

 INFO : ${headers} = {u'content-type': u'application/json'}

INFO : Creating Session using : alias=swagger, url=https://eco-test.nyon-test.de/ebikeconnect/api/app/token/public, headers={u'content-type': u'application/json'},                     cookies=None, auth=None, timeout=None, proxies=None, verify=False,                     debug=0 

 INFO : ${data} = {u'username': '', u'password': u'', u'mobile_id': u''}

 WARN : Deprecation Warning: Use Post Request in the future

  INFO : C:\Python27\lib\site-packages\urllib3\connectionpool.py:858: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings

  InsecureRequestWarning)

 INFO : ${resp} = <Response [400]>

 INFO : {"errors":[{"code":0,"message":"Error; javax.ws.rs.WebApplicationException: ebikeapi.exception.ServiceException: Invalid JSON format (caused by) ebikeapi.exception.ServiceException: Invalid JSON format (caused by) com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 9 (caused by) java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 9"}]}

 FAIL : 400 != 200

Ending test:   Robot.REST TEST.swaggerpost


I guess the request does not take the raw body and so the test gets failed.


Can anyone tell me if I have passed the arguments correctly into the POST request? 

And also, How can I see the actual json response body?

Actual response body :

{

  "token_value":

  "mobile_id":

  "valid_until":

Reply all
Reply to author
Forward
0 new messages