Skip to content
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

[Question] How can I send a POST request? #1260

Closed
martin-thoma opened this issue Sep 8, 2021 · 2 comments · Fixed by #1261
Closed

[Question] How can I send a POST request? #1260

martin-thoma opened this issue Sep 8, 2021 · 2 comments · Fixed by #1261

Comments

@martin-thoma
Copy link

I'm trying to communicate with the Apple Push Notification service (APN) which uses HTTP/2 (docs).

I've (sucessfully) used hyper before for that with code that roughly looks like this:

from hyper import HTTPConnection
import json

bearer_token = "foo"
device_token = "bar"
payload = {}

request_headers = {
    "apns-expiration": "0",
    "apns-priority": "10",
    "authorization": f"bearer {bearer_token}",
}

conn = HTTPConnection("api.development.push.apple.com:443")
conn.request(
    "POST", f"/3/device/{device_token}",
    json.dumps(payload).encode(),
    headers=request_headers
)
resp = conn.get_response()
print(resp.status)
print(resp.reason)
print(resp.read())

How would an equivalent with h2 look like?

@Kriechi
Copy link
Member

Kriechi commented Sep 9, 2021

h2 itself is just a bare-bones sans-io library. Meaning, you need to handle your own I/O operations and understand the HTTP/2 protocol to make sense of the emitted events from h2. A good starting point is also the first code snippet in our main README file and our HTTP client examples: https://python-hyper.org/projects/hyper-h2/en/stable/examples.html#example-clients

If you want a fully-fledged HTTP library to send requests and read responses, I suggest https://www.python-httpx.org/ (which we also recommend as hyper replacement)

@martin-thoma
Copy link
Author

I wanted to use httpx, but it didn't work:

@Kriechi Kriechi closed this as completed Sep 11, 2021
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 a pull request may close this issue.

2 participants