3 ms·
Does using requests buy you much on top of using urllib3 directly? I could look for myself, but it might be more fun as an open discussion topic.
by kstrauser 4y ago
Does using requests buy you much on top of using urllib3 directly?
I could look for myself, but it might be more fun as an open discussion topic.
- xavdid 4y agoErgonomics, mostly. To make a basic POST request w/ a JSON body in urllib3, you write: import json import urllib3 http = urllib3.PoolManager() data = {'key': 'value'} encoded_data = json.dumps(data).encode('utf-8') r = http.request( 'POST', 'http://httpbin.org/post', body=encoded_data, headers={'Content-Type': 'application/json'} ) in requests, it's: import requests requests.post('http://httpbin.org/post', json={'key': 'value'})
- progmetaldev 4y agoDoes the variable "r" in your first example afford you anything more than what you can get from requests.post? e.g. Can you capture a return value from requests.post, and does it give you the same information to help debug that http.request gives?
- xavdid 4y ago> e.g. Can you capture a return value from requests.post Yes, I just didn't in my toy example. That's the response object, which will typically include all the response info, plus reponse.request, which has request info. > and does it give you the same information to help debug that http.request gives? I'm not positive, but I'd bet it's similar.
- SethMLarson 4y agoIn v2.0 urllib3 it's: import urllib3 urllib3.request("POST", "https://httpbin.org/post", json={"key": "value"})
- xavdid 4y agoThat's fair- I was summarizing latest stable: https://urllib3.readthedocs.io/en/1.26.13/user-guide.html#json https://urllib3.readthedocs.io/en/1.26.13/user-guide.html#js...