Manish Gupta
Manish Gupta

Reputation: 4656

python requests Error 415

I am trying to fetch a pdf stream through python requests from flipkart. But on running label.status_code i am getting 415.

My code:

class FlipkartAPI:
    def __init__(self, token):
        self.token = token
        self.session = self.get_session()

    def get_session(self):
        session = requests.Session()
        session.headers.update({'Authorization': 'Bearer %s' % self.token,
            'Content-type': 'application/json',})
        return session

    def fetch_labels(self, orderItemIds):
        self.session.headers.update({'Content-type':'application/octet-stream'})
        url = "https://api.flipkart.net/sellers/orders/labels"
        payload = {'orderItemId':','.join(orderItemIds)}
        return self.session.get(url, params=payload, stream=True)

Function call:

fk = FlipkartAPI(token)
label = fk.fetch_labels(oiids)
print label.status_code
print label.url
print label.content

I get:

415
https://api.flipkart.net/sellers/orders/labels?orderItemId=230005995

The link for documentation is: Documentation I searched on internet and it says the error is for unsupported media type. So what am i doing wrong?

Upvotes: 1

Views: 8787

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1121168

Don't set a Content-Type header, you are sending a GET request, which has no body so has no content to set a type for.

Instead, set an Accept header, as detailed in the documentation. Do not set that header for the whole session, just for this request:

def fetch_labels(self, orderItemIds):
    url = "https://api.flipkart.net/sellers/orders/labels"
    headers = {'Accept': 'application/octet-stream'}
    payload = {'orderItemId':','.join(orderItemIds)}
    return self.session.get(url, params=payload, headers=headers, stream=True)

Upvotes: 1

Related Questions