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 to convert this curl command using --http2 and --cert options to fetch? #899

Closed
cromatikap opened this issue Jul 13, 2020 · 2 comments
Labels

Comments

@cromatikap
Copy link

Hello,

I'd like to convert the following (tested and working) curl command to node-fetch:

curl -v \
  -d '{"aps":{"alert": {"title": "Game Request", "body": "Bob wants to play poker"},"sound":"default"}, "data": "some custom data"}' \
  -H "apns-topic:PROJECT_BUNDLE_IDENTIFIER" \
  -H "apns-expiration: 1" \
  -H "apns-priority: 10" \
  --http2 \
  --cert CERTIFICATE_FILE_PATH:CERTIFICATE_PASS \
  https://api.development.push.apple.com/3/device/DEVICE_TOKEN

Following this article, this is how my code looks like so far:

  const fs = require('fs'), https = require('https'), fetch = require('node-fetch')

  const sslConfiguredAgent = new https.Agent({
    cert: fs.readFileSync(path.resolve(__dirname, '../ApplePushServices.pem'), `utf-8`),
    passphrase: CERTIFICATE_PASS,
    keepAlive: false // switch to true if you're making a lot of calls
  })

fetch('https://api.development.push.apple.com/3/device/DEVICE_TOKEN', {
    method: 'POST',
    headers: {
      'apns-topic': 'PROJECT_BUNDLE_IDENTIFIER',
      'apns-expiration': '1',
      'apns-priority': '10',
      'Content-Type': 'application/json; charset=UTF-8'
    },
    agent: sslConfiguredAgent,
    body: JSON.stringify({
      "aps": {
        "alert": {
          "title": "Game Request",
          "body": "Bob wants to play poker"
        },
        "sound":"default"
      },
      "data": "some custom data"
    })
  })
  .then(async res => {console.log("OK", res)})
  .catch(err => {console.log("ERROR", err)});

I get this result:

ERROR FetchError: request to https://api.development.push.apple.com/3/device/xxxxxx failed, reason: Parse Error: Expected HTTP/
    at ClientRequest.<anonymous> (/Users/bidetaggle/Desktop/boxbeat-media-server/server/node_modules/node-fetch/lib/index.js:1455:11)
    at ClientRequest.emit (events.js:315:20)
    at TLSSocket.socketOnData (_http_client.js:514:9)
    at TLSSocket.emit (events.js:315:20)
    at addChunk (_stream_readable.js:302:12)
    at readableAddChunk (_stream_readable.js:278:9)
    at TLSSocket.Readable.push (_stream_readable.js:217:10)
    at TLSWrap.onStreamRead (internal/stream_base_commons.js:186:23) {
  type: 'system',
  errno: 'HPE_INVALID_CONSTANT',
  code: 'HPE_INVALID_CONSTANT'
}

What am I missing in here?

My Environment

software version
node-fetch ^2.6.0
node v14.4.0
npm 6.14.4
Operating System Mac OS Catalina
@tinovyatkin
Copy link
Member

node-fetch doesn't support HTTP/2 at the moment. If that is the requirement then you may look to different projects, like https://github.com/JCMais/node-libcurl or https://github.com/grantila/fetch-h2

@cromatikap
Copy link
Author

Hi, thank you for your help. Because I also have to handle --cert option I decided to run directly curl from the system to go ahead faster. I see that http2 is on the v3 roadmap.
I link the issue here as a vote for the feature: #668

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

No branches or pull requests

2 participants