Skip to content

Instantly share code, notes, and snippets.

@kangasta
Last active December 17, 2020 21:10
Show Gist options
  • Select an option

  • Save kangasta/61ed31a567014e2da8e0699f077463d6 to your computer and use it in GitHub Desktop.

Select an option

Save kangasta/61ed31a567014e2da8e0699f077463d6 to your computer and use it in GitHub Desktop.
Use python requests with cookies synced with RF SeleniumLibrary
from requests import request
from robot.api.deco import keyword
from robot.libraries.BuiltIn import BuiltIn
@keyword
def send_http_request(method, url, return_value='json', **kwargs):
'''Send requests with cookies synced with RF SeleniumLibrary
Wrapper for requests.request to send requests with current cookies from Robot Framework SeleniumLibrary. Keyword also updates cookies from response to the SeleniumLibrary.
Args:
method: Passed through to the requests.request function.
url: Passed through to the requests.request function.
return_value: Defines the return value type: 'json', 'utf-8', or anything else to get content as bytes. 'json' by default.
kwargs: Passed through to the requests.request function.
Returns:
Response content as dict, str, or bytes depending on return_value parameter value.
Raises:
HTTPError: On non-ok response status code
'''
cookies = BuiltIn().run_keyword('Get Cookies', 'True')
response = request(method, url, cookies=cookies, **kwargs)
response.raise_for_status()
for c in response.cookies:
BuiltIn().run_keyword(
'Add Cookies',
c.name,
c.value,
c.path,
c.domain,
c.secure,
c.expires,
)
if return_value == 'json':
return response.json()
elif return_value == 'utf-8':
return response.content.decode('utf-8')
return response.content
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment