问题
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