Skip to content

When sending data get the bytes length not the string length #112

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 3, 2022

Conversation

Neradoc
Copy link
Contributor

@Neradoc Neradoc commented Aug 3, 2022

This always converts data from string to bytes before getting the length.
Otherwise data that contains UTF-8 characters gets truncated if the server follows the content length.
data is still converted to bytes if not a string. (Is it necessary ?)

This was reported in the support forums by testing with pushover.net (using the desktop in-browser interface to receive the messages).

import ssl
import time
import wifi
import socketpool
import adafruit_requests
from secrets import secrets

# URLs
PUSHOVER_JSON_URL = 'https://api.pushover.net/1/messages.json'

wifi.radio.connect(secrets['ssid'], secrets['password'])
pool = socketpool.SocketPool(wifi.radio)
requests = adafruit_requests.Session(pool, ssl.create_default_context())

data = {
    'token': secrets['pushover_app_token'],
    'user': secrets['pushover_user_key'],
    'title': "Test Requests Content Length",
    'message': 'The ñ temperature is, 32°C...',
}
with requests.post(PUSHOVER_JSON_URL, data=data) as response:
    print("API Response: ", response.text)

ñ and ° are encoded with 2 bytes.

>>> len('The ñ temperature is, 32°C...')
29
>>> len('The ñ temperature is, 32°C...'.encode())
31

This is the result in the browser, before the fix is the bottom one. 2 characters are missing.

Capture d’écran 2022-08-03 à 14 40 40

Note that what gets cut depends on the order in which the parameters end up, dicts are not ordered in CP so the loop goes through them in no particular order. In this example it's the message that ends up last.

I only tested that, and some of the examples from this repository.

@tekktrik tekktrik requested a review from a team August 3, 2022 14:12
@ladyada ladyada requested a review from dhalbert August 3, 2022 15:03
Copy link
Contributor

@dhalbert dhalbert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good and thanks for fixing the unnecessary conversion to bytes as well.

@dhalbert dhalbert merged commit 6b72737 into adafruit:main Aug 3, 2022
adafruit-adabot added a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request Aug 4, 2022
Updating https://github.com/adafruit/Adafruit_CircuitPython_MAX31856 to 0.10.0 from 0.9.11:
  > Added Black formatting badge
  > Merge pull request adafruit/Adafruit_CircuitPython_MAX31856#19 from eirinnm/non-blocking
  > Changed .env to .venv in README.rst

Updating https://github.com/adafruit/Adafruit_CircuitPython_Requests to 1.12.5 from 1.12.4:
  > Merge pull request adafruit/Adafruit_CircuitPython_Requests#112 from Neradoc/fix-send-content-length
  > Added Black formatting badge
  > Changed .env to .venv in README.rst
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants