机器人框架传递cookie以获取请求(RequestsLibrary)-TypeError [英] Robot framework pass cookie to get request (RequestsLibrary) - TypeError
问题描述
我需要将Cookie值传递给Get request关键字才能使其正常工作,但是仍然失败.我不确定如何正确地将其传递.关键字的文档说明了有关标题的信息:标题:与请求一起使用的标题字典"
我已经使用库RequestsLibrary
进行了这样的测试:
I need to pass cookie value to Get request keyword to make it work, but it is still failing. I am not sure how to pass it correctly there. Documentation of the keyword says about the headers: "headers: a dictionary of headers to use with the request"
I have test like this, using Library RequestsLibrary
:
Check request and response
Do Local Login ${username} ${password} #this is my custom keyword - opens browser and logs in using UI
${cookie_value}= Get Cookie Value JSESSIONID
Create Session session1 https://oururl.com verify=${False} cookies=${cookie_value}
${dict}= Create Dictionary Cookie=${cookie_value}
${resp}= Get request session1 /somepage/request headers=${dict}
它总是返回TypeError: string indices must be integers
.
追溯:
Traceback (most recent call last):
File "c:\python27\lib\site-packages\RequestsLibrary\RequestsKeywords.py", line 370, in get_request
session, uri, params, headers, json, redir, timeout)
File "c:\python27\lib\site-packages\RequestsLibrary\RequestsKeywords.py", line 897, in _get_request
verify=self.verify)
File "c:\python27\lib\site-packages\requests\sessions.py", line 501, in get
return self.request('GET', url, **kwargs)
File "c:\python27\lib\site-packages\requests\sessions.py", line 474, in request
prep = self.prepare_request(req)
File "c:\python27\lib\site-packages\requests\sessions.py", line 385, in prepare_request
cookies = cookiejar_from_dict(cookies)
File "c:\python27\lib\site-packages\requests\cookies.py", line 518, in cookiejar_from_dict
cookiejar.set_cookie(create_cookie(name, cookie_dict[name]))
我真的需要将cookie传递给请求,因为否则它将再次仅返回登录页面.
And I really need to pass the cookie to the request, because otherwise it returns back only login page again.
非常感谢您的帮助!
推荐答案
Sooo终于找到了解决方案:
Sooo finally found the solution:
Do Local Login ${username} ${password}
${cookie_value}= Get Cookie Value JSESSIONID
${dict}= Create Dictionary JSESSIONID=${cookie_value}
Create Session session1 https://oururl.com verify=${False} cookies=${dict}
${resp}= Get request session1 /somepage/request
关键字Create Session
与cookie一起使用,不仅必须将cookie作为字典值传递,而且还必须将字典JSESSIONID=${cookie_value}
传递给cookie.
另外,我不必在Create Session
中传递cookie,但在Get Request
中传递,只是我必须将cookie制成像${dict}=
Create Dictionary Cookie=JSESSIONID=${cookie_value}
这样的字典,然后传递给Get request session1 /somepage/request headers=${dict}
Keyword Create Session
works with the cookie, just the cookie must be passed not just as a cookie value, but dictionary JSESSIONID=${cookie_value}
.
Alternatively I don't have to pass the cookie in Create Session
but in Get Request
, just then I have to make the cookie as dictionary like ${dict}=
Create Dictionary Cookie=JSESSIONID=${cookie_value}
and this pass into Get request session1 /somepage/request headers=${dict}
这篇关于机器人框架传递cookie以获取请求(RequestsLibrary)-TypeError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!