HTTP Requests in Python using Requests(GET,POST,PUT,DELETE)

response = requests.get('https://httpbin.org/get')
print(response.textual content)
#Outcome
{
"args":
"key1": "value1",
"key2": "value2"
,...
response = requests.get('https://httpbin.org/get')
print(response.content material)
# Outcome
b'{n "args": n "key1": "value1", n "key2": "value2"n , n "headers": {n "Settle for": "*/*", n
"Settle for-Encoding": "gzip, deflate",...
response = requests.get('https://httpbin.org/get')
print(response.json())
#Outcome
{'args': 'key1': 'value1', 'key2': 'value2', 'headers': {'Settle for': '*/*', 'Settle for-Encoding': 'gzip, deflate', 'Host': 'httpbin.org', 'Consumer-Agent'...
response = request.get(url_to_download_large_file, stream=True)
with open(filename, 'wb') as f:
for chunk in response.iter_content(chunk_size=128):
f.write(chunk)
response = requests.get('https://httpbin.org/get')
print(response.encoding)
#Outcome
utf-8
#Reassign a brand new encoding
response.encoding = 'ISO-8859-1'
  • elapsed_time : The time elapsed between sending a request to receiving the response.
  • url : Offers the precise URL from which the response is shipped. That is helpful in figuring out if the request was redirected.
  • cookies : This property incorporates the cookies despatched again by the server.
  • headers : The headers despatched by the server.
  • historical past : This property is used to test if the request was redirected. The property will include an inventory of Response objects created to finish the request ranging from oldest to latest.
    Observe: To keep away from redirect, we are able to cross in allow_redirects=False within the request.
params = 'key1': 'value1', 'key2': 'value2'
response = requests.get("https://httpbin.org/get", params=params)
print(response.request.__dict__)
#Outcome
{'methodology': 'GET', 'url': 'https://httpbin.org/get?key1=value1&key2=value2', 'headers': {'Consumer-Agent': 'python-requests/2.26.0'...

More Posts