Content-Type请求示例

Content-Type请求示例 1.application/json 请求 python代码示例 import requests import json data = { 'a': 123, 'b': 456 } ## headers中添加上content-type这个参数,指定为json格式 headers = {'Content-Type': 'application/json'} ## post的时候,将data字典形式的参数用json包转换成json格式。 response = requests.post(url='http://127.0.0.1:9000/api/ttt', headers=headers, data=json.dumps(data)) 实际发出的请求体 POST /api/ttt HTTP/1.1 Host: 127.0.0.1:9000 User-Agent: python-requests/2.31.0 Accept-Encoding: gzip, deflate Accept: */* Connection: keep-alive Content-Type: application/json Content-Length: 20 {"a": 123, "b": 456} 2. application/x-www-form-urlencoded 请求 python代码示例 import requests data = { 'a': 123, 'b': 456 } ## headers中添加上content-type这个参数,指定为x-www-form-urlencoded格式 headers = {'Content-Type': 'application/x-www-form-urlencoded'} ## post的时候,默认按x-www-form-urlencoded格式处理 response = requests.post(url='http://127.0.0.1:9000/api/ttt', headers=headers, data=data) 实际发出的请求体 POST /api/ttt HTTP/1.1 Host: 127.0.0.1:9000 User-Agent: python-requests/2.31.0 Accept-Encoding: gzip, deflate Accept: */* Connection: keep-alive Content-Type: application/x-www-form-urlencoded Content-Length: 11 a=123&b=456

November 12, 2024 · 1 min

requests 库的另类用法(stream)

版权声明 本站原创文章 由 萌叔 发表 转载请注明 萌叔 | http://vearne.cc 起因: 同事让我帮他抓取一批URL,并获取对应URL的<title>标签中的文字,忽略对应URL网站的封禁问题,这个任务并不是一个特别麻烦的事情。然后实际跑起来,却发现流量打的很高,超过10Mb/s。 经过排查发现,是因为很多URL,实际是下载链接,会触发文件下载,这些URL对应的html中根本不会包含<title>标签,那么处理逻辑就很清晰了,先拿到headers,取出Content-Type,判断是否是 text/html,如果不是,则该Response的body体,就没有必要读取了。 查找requests的相应资料 By default, when you make a request, the body of the response is downloaded immediately. You can override this behaviour and defer downloading the response body until you access the Response.content attribute with the stream parameter: tarball_url = &#039;https://github.com/kennethreitz/requests/tarball/master&#039; r = requests.get(tarball_url, stream=True) At this point only the response headers have been downloaded and the connection remains open, hence allowing us to make content retrieval conditional: ...

January 1, 2018 · 1 min