Getting 401 Response from RIDE editor on using user-defined function(in Python) while the same keyword shows correct response on calling from PyCharm Editor

123 views
Skip to first unread message

RoboUser20

unread,
Dec 27, 2018, 7:28:34 AM12/27/18
to robotframework-users
Hi all,

I have to automate REST API call for my project application

Using RIDE editor for Robot framework
Robot Framework version 3.0.4
Python version 2.7.13


TestSuit1.robot

*** Settings ***
Suite Setup
Library           String
Library           OperatingSystem
Library           requests
Library           json
Library           ../CustomLibrary/Generic.py                                 #### here I am importing python file having user defined functions
Library           ../CustomLibrary/RequestPayload.py
Library           Collections
Library           Process

*** Variables ***


*** Test Cases ***
CreateUser
    [Tags]    CreateUser
    ${header}=    Get header
    ${payload}=    get payload
    ${createUser}=    create user post    http://127.0.01/test/device    ${COOKIE}    ${payload}    ${header}
    Log    ${createUser}
    Should Be Equal As Strings    ${createUser.status_code}    200

***Keywords***
    ${COOKIE}=    Cookie From Response    http://172.0.01/test/cookie    {"password": "test","username": "test"}
    Set Global Variable    ${COOKIE}


Generic.py

import requests
import json
import re

'''
Fetch Session cookie from login Request
'''


def cookie_from_response(url, payload):
s = requests.Session()
response = s.post(url, data=payload)
cookies = response.cookies.get_dict()
# print ("hello"+str(s.cookies))
out = re.search(r'(JSESSIONID=.*)? for(.*)?', str(s.cookies))
# print out.group(1)
l = out.group(1).split("=")
# print l
c = {l[0]: l[1]}
# print c
return c


'''
Get headers for POST, GET, DELETE request
'''


def get_header():
header = {"Content-Type": "application/json", "Accept": "application/json"}
return header


'''
POST Request with args: url, data, cookies, header
'''


def create_user_post(url, cookie, payload, header):
response = requests.post(url, data=payload, cookies=cookie, headers=header)
print(response.status_code)
return response




Query: So on running TestSuite1.robot, 401 response is coming while if same function "create_user_post" is called by passing its arguments in python file, 200 response is returned.

Paolo De Grazia

unread,
Dec 27, 2018, 5:58:48 PM12/27/18
to robotframework-users
401 means unauthorised, which would make me think about the cookies.
Start investigating your issue by logging the value of ${Cookies} in the test case.
My guess is it will be None.
I can spot one mistake in the keywords section:
You should name your keyword something like "set cookie", indent the rest, then add that keyword in the test case before making the call.
Try to log again your cookies, right now they should be populated.

Disclaimer: I haven't tested my solution

Reply all
Reply to author
Forward
0 new messages