Skip to content
This repository was archived by the owner on May 29, 2025. It is now read-only.

Set encoding to UTF-8 when no encoding is specified #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

phihag
Copy link

@phihag phihag commented Jul 20, 2020

PayPal's actual responses just say Content-Type: application/json, causing requests to guess the encoding. This is wrong; the data from PayPal is always UTF-8.
Therefore, default to UTF-8 when no encoding is set.

Demonstration of the problem:


import paypalhttp

class PseudoRequest:
    verb = 'GET'
    path = '/2020/paypal.json'
    headers = {'Content-Type': 'application/json'}
    body = None

class PseudoEnvironment:
    base_url = 'https://phihag.de'

environment = PseudoEnvironment()
client = paypalhttp.HttpClient(environment)
res = client.execute(PseudoRequest())
s = res.result.payer.address.admin_area_1

assert 'ü' in s, s

PayPal's actual responses just say `Content-Type: application/json`, causing requests to guess the encoding. This is wrong; the data from PayPal is always UTF-8.
Therefore, default to UTF-8 when no encoding is set.

Demonstration of the problem:

```

import paypalhttp

class PseudoRequest:
    verb = 'GET'
    path = '/2020/paypal.json'
    headers = {'Content-Type': 'application/json'}
    body = None

class PseudoEnvironment:
    base_url = 'https://phihag.de'

environment = PseudoEnvironment()
client = paypalhttp.HttpClient(environment)
res = client.execute(PseudoRequest())
s = res.result.payer.address.admin_area_1

assert 'ü' in s, s
```
@cschlueter
Copy link

cschlueter commented Jul 23, 2020

I'm running into the same problem. While executing an OrdersGetRequest with an Umlaut inside the shipping address. This should warrant an urgent fix, because I can imagine a lot of people run into this problem.

Furthermore, I can validate the proposed fix on my side, after changing the line manually, locally in the braintree http library it works as expected. Obviously not a solution for production.

@phihag any way to work around this issue without a fix in the library? I'm a little lost.

@phihag
Copy link
Author

phihag commented Jul 23, 2020

@cschlueter Instead of making an instance of PayPalHttpClient, make an instance of the following class:

class FixedEncodingPayPalHttpClient(PayPalHttpClient):
    # Workaround for https://www.paypal-support.com/s/case/5002E00001bw3sxQAA/ /
    # https://github.com/paypal/paypalhttp_python/pull/5
    def parse_response(self, response):
        response.encoding = response.encoding or 'utf-8'
        return super().parse_response(response)

@cschlueter
Copy link

@cschlueter Instead of making an instance of PayPalHttpClient, make an instance of the following class:

class FixedEncodingPayPalHttpClient(PayPalHttpClient):
    # Workaround for https://www.paypal-support.com/s/case/5002E00001bw3sxQAA/ /
    # https://github.com/paypal/paypalhttp_python/pull/5
    def parse_response(self, response):
        response.encoding = response.encoding or 'utf-8'
        return super().parse_response(response)

Ah, perfect, thanks so much for the workaround.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants