Python session.put return 415: Unsupported Media Type

45 Views Asked by At

I do not know why I receive the respone: "415: Unsupported Media Type"

I try to put new values. When I get some values it works fine.

code snippet:

headers = {}
headers["charset"] = "utf-8"
headers["Accept-Language"] = "pl-PL"
headers["Content-Type"] = "application/json"

headers["Accept"] = "application/vnd.allegro.public.v1+json"
headers["Authorization"] = "Bearer {}".format(access_token)

with requests.Session() as session:
    session.headers.update(headers)
    DEFAULT_API_URL = "https://api.allegro.pl"
    payload = {"maxAmount": {"amount": "230.00", "currency": "PLN"}}

    response = session.put(
        url=DEFAULT_API_URL + "/bidding/offers/1498090700/bid",
        json=json.dumps(payload),
    )

    print(f"{response.status_code}: {response.reason}")
    print(response.json())

Response: "415: Unsupported Media Type"

1

There are 1 best solutions below

2
AKX On

According to the API documentation, the content type of the request also needs to be application/vnd.allegro.public.v1+json. Try e.g.

ALLEGRO_JSON = "application/vnd.allegro.public.v1+json"
DEFAULT_API_URL = "https://api.allegro.pl"
headers = {
    "Accept-Language": "pl-PL",
    "Content-Type": ALLEGRO_JSON,
    "Accept": ALLEGRO_JSON,
    "Authorization": f"Bearer {access_token}",
}

with requests.Session() as session:
    payload = {"maxAmount": {"amount": "230.00", "currency": "PLN"}}

    response = session.put(
        url=DEFAULT_API_URL + "/bidding/offers/1498090700/bid",
        json=payload,
        headers=headers,
    )

    print(f"{response.status_code}: {response.reason}")
    print(response.json())