Phil-east
Phil-east

Reputation: 121

CURL HTTP2 request

I would like to know if someone successfully managed to send a push notification through the new APNS API (HTTP2)using CURL.

An example request's given on the APNs Provider API page

Here's how the request must be:

HEADERS

\- END_STREAM

\+ END_HEADERS

:method = POST

:scheme = https

:path = /3/device/00fc13adff785122b4ad28809a3420982341241421348097878e577c991de8f0

host = api.development.push.apple.com

apns-id = eabeae54-14a8-11e5-b60b-1697f925ec7b

apns-expiration = 0

apns-priority = 10

content-length = 33

DATA

\+ END_STREAM

{ "aps" : { "alert" : "Hello" } }

But with the following command I get error "curl: (16) HTTP/2 stream 1 was not closed cleanly: error_code = 8":

curl \

--verbose \

--http2 \

--cert <APPLICATION_CERT_FILE> \

--key <APPLICATION_KEY_FILE> \

--header "Content-Type: application/json" \

--header ":method: POST" \

--header ":path: /3/device/<DEVICE ID>" \

--data '{ "aps" : { "alert" : "Hello" } }' \

https://api.development.push.apple.com

Any tips?

Upvotes: 12

Views: 26891

Answers (3)

judepereira
judepereira

Reputation: 1297

Certificate based provider authentication

curl -v
     -d '{"aps":{"alert":"hello"}}'
     -H "apns-topic: <your app bundle ID>" 
     --http2 
     --cert cert.pem
     https://api.push.apple.com/3/device/<device token>

Token based provider authentication

curl -v 
     -d '{"aps":{"alert":"hello"}}'
     -H "apns-topic: <your app bundle ID>" 
     -H "authorization: bearer xxxx.yyyy.zzzz" 
     --http2
     https://api.push.apple.com/3/device/<device token>

You will need to generate your JWT token and sign it using ES256. This is out of scope here (it's easy to find many libraries on a quick Google search).

Upvotes: 13

ryebread
ryebread

Reputation: 984

I have successfully sent push notifications from cURL using the following:

curl -v -d '{"aps":{"alert":"Test Push","sound":"default"}}' \
--cert /path/to/cert/cert.pem:SECURE_PASSWORD \
-H "apns-topic: com.app.identifier" --http2 \
https://api.development.push.apple.com/3/device/DEVICE_ID

This is using curl version 7.48.0, installed by homebrew:

$ curl --version
curl 7.48.0 (x86_64-apple-darwin15.4.0) libcurl/7.48.0 OpenSSL/1.0.2g zlib/1.2.5 nghttp2/1.9.1
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp smb smbs smtp smtps telnet tftp 
Features: IPv6 Largefile NTLM NTLM_WB SSL libz TLS-SRP HTTP2 UnixSockets 

Notice, however, that I get the same error as you do when using our development certificate on the production server: https://api.push.apple.com/

curl: (16) HTTP/2 stream 1 was not closed cleanly: error_code = 8

Upvotes: 17

Daniel Stenberg
Daniel Stenberg

Reputation: 58024

  1. remove --header ":method: POST" (--data will make it use POST)

  2. remove -header ":path: /3/device/"

    The :path part is what you want on the right side of the host name in the URL, so specify a URL like perhaps https://api.development.push.apple.com/3/device/<DEVICE ID>

  3. That said, getting a HTTP/2 level stream error like that is very unexpected and would rather indicate a lower level problem somewhere...

Upvotes: 0

Related Questions