寻找cookie之旅

公司接口自动化,需要用到cookie。这个cookie可以从login的响应header中取到。

image-20190815172510232

直接用python调这个接口,返回的响应header是这个:

{'Date': 'Thu, 15 Aug 2019 09:05:36 GMT', 'Content-Type': 'text/html', 'Transfer-Encoding': 'chunked', 'Connection': 'keep-alive', 'Server': 'nginx-clojure', 'Vary': 'Accept-Encoding', 'ETag': 'W/"1424-1565676334000"', 'Last-Modified': 'Tue, 13 Aug 2019 06:05:34 GMT', 'Content-Encoding': 'gzip'}

没有看到预期的数据。

刚好公司Jmeter已经有现成的调用,看了下View Results Tree:

image-20190815173205895

懵!为啥请求了5次。就在花了大半天时间调参数调header调content-type均无果,一筹莫展之际,我网上搜索了一下:

image-20190815173355910

那三个字一下就从屏幕刺到了我的双眼!

302!重定向啊!学艺不精啊!

于是乎点了下Jmeter的调用结果,登录0-3果然都是302,登录4才是200。

image-20190815173558918

这个cookie就藏在登录-0的header里面:

image-20190815173808622

于是python就好处理了:

# r为requests.get()返回
r.history[0].headers

这样就能拿到第1次302重定向的headers。

把r.history打印出来

print(r.history)
# [<Response [302]>, <Response [302]>, <Response [302]>, <Response [302]>]

可以看到确实发生了4次302重定向。

版权申明:本文为博主原创文章,转载请保留原文链接及作者。

posted @ 2019-10-27 15:11  测试开发刚哥  阅读(140)  评论(0编辑  收藏  举报