How to add headers for CreateSession in robot framework HTTP requests library

眉间皱痕 提交于 2019-11-30 18:56:38

问题


I am using the requests library in Robot framework provided at this github link. The documentation implies that if can send custom headers by doing CreateSession <url> headers={'header1':'value1'} .... However, when I do that I get an error "ValueError: need more than 1 value to unpack"

This works

CreateSession SendCustomHeader http://myhost.com  verify=False 

This does not

CreateSession SendCustomHeader http://myhost.com headers={'header1':'value1'} verify=False

I tried with various combinations of headers='header1' OR {'header1':'value1'} OR 'header1':'value1' with same error

There does not seem to be an error in the requests library code RequestsKeywords.py

"        self.builtin.log('Creating session: %s' % alias, 'DEBUG')
        s = session = requests.Session()
        s.headers.update(headers)
"

I am not sure where the error is coming from and am therefore unable to fix

Any pointers to troubleshoot are appreciated


回答1:


You aren't passing a dictionary, you're passing a string that looks like a dictionary. The solution is to create a proper dictionary and pass that in. Robot has a Create Dictionary keyword for this purpose.

*** Settings ***
| Library | Collections

*** Test Cases ***
| Example
| | ${headers}= | Create dictionary
| | ... | header1 | value1
| | ... | header2 | value2
| | CreateSession | SendCustomHeader | http://myhost.com  
| | ... | header=${headers} | verify=False 



回答2:


According to the Requests documentation, you may add headers to the Session object as:

s = requests.Session()
s.auth = ('user', 'pass')
s.headers.update({'x-test': 'true'})


来源:https://stackoverflow.com/questions/30075809/how-to-add-headers-for-createsession-in-robot-framework-http-requests-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!