Skip to content

Issue with sending header values twice #148

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

Closed
justmobilize opened this issue Dec 28, 2023 · 7 comments
Closed

Issue with sending header values twice #148

justmobilize opened this issue Dec 28, 2023 · 7 comments

Comments

@justmobilize
Copy link
Collaborator

When helping a member in the discord channel #help-with-circuitpython, I had made the mistake of thinking that this library didn't automatically deal with turning a dict into a urlencoded string. I had them convert it by hand and try and it worked.

As it turns out - it does, but also sends the header Content-Type: application/x-www-form-urlencoded, which some systems (Spotify as one) can't handle getting the header twice.

I would like to open a PR to check the header for an existing Content-Type before adding it's own (both for urlencoded and json). It would implement a method to check for an existing value since we would also want to check for casing mis-matches.

@brentru
Copy link
Member

brentru commented Jan 25, 2024

@justmobilize I am re-opening this issue as #149 is causing a "TypeError: wrong number of arguments" error as seen here: adafruit/Adafruit_CircuitPython_AdafruitIO#115. I am also able to locally reproduce this.

The issue appears to be with doing a GET with only headers like in adafruit_io.py's get function:

    def _get(self, path: str):
        """
        GET data from Adafruit IO

        :param str path: Formatted Adafruit IO URL from _compose_path
        """
        with self._http.get(
            path, headers=self._create_headers(self._aio_headers[1])
        ) as response:
            self._handle_error(response)
            json_data = response.json()
        return json_data

Tracing through this, your new send_header expects (self, socket, header, value). However, in this case, the header is the X-AIO-KEY and the value is of None type.

On L561, in send_header, the value is sent via _send_as_bytes but it is a NoneType where send_as_bytes expects a String:
self._send_as_bytes(socket, value)

@justmobilize Would you be able to resolve this? Do you want me to resolve this?

@justmobilize
Copy link
Collaborator Author

@brentru I'm happy to take a look at this

@justmobilize
Copy link
Collaborator Author

justmobilize commented Jan 26, 2024

@brentru looking, my PR just changed from .encode to bytes

Old:
image

Looking at CPython requests:
image

If it's None it just doesn't send it, and if it's not a str it raises.

Looking into their code they:

  1. Drop on None
  2. Error on non str|bytes

I'll get a PR opened with tests in the next day or so.

I might recommend Adafruit_CircuitPython_AdafruitIO also add an exception here to let the user know the data is missing (otherwise I assume they would just get an auth error after the fact).

@justmobilize
Copy link
Collaborator Author

Code change here: #152. Want to make sure the method makes sense, then will add tests

@justmobilize
Copy link
Collaborator Author

@brentru just waiting on an initial code review

@justmobilize
Copy link
Collaborator Author

@brentru my code fix is merged. Can you confirm it fixes the issue and close this?

@justmobilize
Copy link
Collaborator Author

Scratch that. I see the user already tested. Closing.

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

No branches or pull requests

2 participants