gopaycommunity / gopay-python-api

GoPay's Python SDK for Payments REST API
https://doc.gopay.com
MIT License
13 stars 13 forks source link

requests.exceptions.InvalidHeader: Header part (<ContentType.JSON: 'application/json'>) from {'Content-Type': <ContentType.JSON: 'application/json'>} must be of type str or bytes, not <enum 'ContentType'> #23

Closed Salamek closed 3 weeks ago

Salamek commented 7 months ago

Crash is caused by passing ContentType(StringEnum) do requests headers dict here: https://github.com/gopaycommunity/gopay-python-api/blob/066e50885f2e424f513ad4ea931da36386975f04/gopay/api.py#L62

(PR coming soon)

geeshta commented 3 weeks ago

Can you give a MCVE for how to cause it to crash? It passes unit tests. You can use a value of class StrEnum(str, Enum) as a string. This also works and doesn't crash:

from gopay.enums import ContentType
import requests

content_type = ContentType.JSON

r = requests.post(
    "https://gw.sandbox.gopay.com/api/payments/payment",
    headers={"Content-Type": content_type},
    data={"test": "test"}    
)

assert isinstance(content_type, str)
Salamek commented 3 weeks ago

I'm unable to reproduce this now on Python 3.12.5 and 3.11.2, this was issue on some previous Python3 or requests (etc.) version, servers are all updated now...

When i'm looking at gopay code bit deeper i can see that StrEnum is instance of str with __str__ defined so that should work and pass requests header tests ok:

https://github.com/psf/requests/blob/a3ce6f007597f14029e6b6f54676c34196aa050e/src/requests/utils.py#L1048-L1057

I did not "make up" this Exception (and its fix/patch that was used in production on my part until deps. update, when it started working without patching gopay i just thought this issue got resolved :smiling_face_with_tear: ) so there is some combination of gopay/python/requests where it fails for some reason... BUT it is 6 months that this was not an issue for anyone else, it works for me RN with ~latest repo Python3 in Debian/Archlinux and deps. And i don't have time to bisect this any more, so i think we can close this issue as resolved and reject attached PR.

geeshta commented 3 weeks ago

@Salamek Thank you.