Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

request has no content-length

Tags:

python

request

it seems every now and then i get a bad response, i see that in the header content-legth:0 but it seems i only get the content-length in the header wen it is a bad response, shoudn't i always get the content-length in a response? here is my code

import requests
import json
from pprint import pprint
import time 

def read_OSRS_GE(item_id):
    api_url="http://services.runescape.com/m=itemdb_oldschool/api/catalogue/detail.json?item="
    # get all items and put em in file
    # print(api_url+str(item_id))
    r=requests.get(api_url+str(item_id))
    print(r)
    header=r.headers
    print(header)
    data=r.json()
    # print("dataid",data["item"]["id"])
    # pprint(data)
    row=[]
    row.append(int(time.time()))
    row.append(data["item"]["id"])
    row.append(data["item"]["name"])
    row.append(data["item"]["current"]["price"])
    row.append(data["item"]["members"])
    print(row)
    return
def get_items():
    api_url="https://rsbuddy.com/exchange/summary.json"
    #get all items and put em in file
    # print(api_url)
    r=requests.get(api_url)
    data=r.json()
    # pprint(data)
    counter=1
    for key, value in data.items():
        print("item id:",key)
        print("call:",counter)
        read_OSRS_GE(key)

        counter+=1

    return
get_items()
like image 722
extreme4all Avatar asked May 11 '26 22:05

extreme4all


1 Answers

You could get the size of the raw response in bytes from len(r.content)

The response may not necessarily have the Content-Length header set.

The response from the example you provided currently contains a Transfer-Encoding field. The data is sent in a series of chunks, so the Content-Length header is emitted.

MDN: Transfer-Encoding

like image 133
Wes Doyle Avatar answered May 14 '26 13:05

Wes Doyle



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!