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

[Deno] Invalid JSON received from the Stripe API #1519

Closed
thorwebdev opened this issue Aug 22, 2022 · 9 comments · Fixed by #1527
Closed

[Deno] Invalid JSON received from the Stripe API #1519

thorwebdev opened this issue Aug 22, 2022 · 9 comments · Fixed by #1527
Labels

Comments

@thorwebdev
Copy link

Describe the bug

stripe.accounts.createLoginLink throws error when executed in Deno environment.

To Reproduce

  1. Create index.ts file
  2. Add code
  3. Run via Deno
  4. Get error

Expected behavior

Request to execute successfully

Code snippets

// errors
const loginLink = await stripe.accounts.createLoginLink('acct_1LZWJGPPxRBiNZOx')

// works fine
const loginLink = await fetch(
  `https://api.stripe.com/v1/accounts/${'acct_1LZWJGPPxRBiNZOx'}/login_links`,
  {
    method: 'POST',
    headers: {
      Authorization: `Bearer ${Deno.env.get('STRIPE_API_KEY')}`,
    },
  }
).then((res) => res.json())

OS

macOS 11.06.1

Node version

Deno version: { deno: "1.20.3", v8: "10.0.139.6", typescript: "4.6.2" }

Library version

https://esm.sh/[email protected]?target=deno&no-check

API version

2020-08-27

Additional context

Related discussion on StripeDev Discord

Potentially related to #954 ?

@thorwebdev thorwebdev added the bug label Aug 22, 2022
@dcr-stripe
Copy link
Contributor

Thanks for the report @thorwebdev and sorry you're having issues! I'm able to reproduce this as well. Going to investigate and report back!

@dcr-stripe
Copy link
Contributor

For some reason the response for these calls when issued from Deno is actually returning a 400 without the usual JSON content:

<html>
<head><title>400 Bad Request</title></head>
<body>
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx</center>
</body>
</html>

This causes the error handler to throw. I don't have the same behavior in Node.

@dcr-stripe
Copy link
Contributor

This seems to be caused specifically due to sending Content-Length: 0 for this request. If I remove this header internally, then Deno can send the request successfully and gets an appropriate response. 🤔

@dcr-stripe
Copy link
Contributor

dcr-stripe commented Aug 22, 2022

This is reproducible with:

const loginLink = await fetch(
  `https://api.stripe.com/v1/accounts/<account id>/login_links`,
  {
    method: 'POST',
    headers: {
      Authorization: `Bearer ${Deno.env.get('STRIPE_API_KEY')}`,
      "Content-Length": 0,
    },
  }
).then((res) => res.json())

This throws an error and seems to be Deno specific. If I try to do this with the Node 18+ fetch implementation, there is no error.

Sending the header is appropriate as per the spec:

A user agent SHOULD send Content-Length in a request when the method defines a meaning for enclosed content and it is not sending Transfer-Encoding. For example, a user agent normally sends Content-Length in a POST request even when the value is 0 (indicating empty content).

@dcr-stripe
Copy link
Contributor

The root cause issue here is Deno injects its own Content-Length header despite stripe-node providing a value for it, so the end request has this header twice and nginx returns a 400. I've filed denoland/deno#15545 on the Deno side for this.

I'll investigate to see if we can fix this from the Stripe side in the interim.

@dcr-stripe
Copy link
Contributor

Hi @thorwebdev - thanks for the patience here!

I've released the fix under [email protected] . It now works for me. Could you try it out with your code just to double check, and if all works, I'll release it to the main stable channel?

You can try this out using:

import Stripe from "https://esm.sh/[email protected]?target=deno";

Thanks!

@thorwebdev
Copy link
Author

@dcr-stripe I can confirm that import Stripe from 'https://esm.sh/[email protected]?target=deno&no-check' is working for me both locally and when deployed to Supabase/Deno Deploy. Thanks so much for the fast turnaround!

@dcr-stripe
Copy link
Contributor

This is now released as [email protected] (https://github.com/stripe/stripe-node/releases/tag/v10.5.0) - thanks for the patience here!

@mehulmathur16
Copy link

Hi, I'm getting this error on deployment. It works great on local though.

Node version : 14.17.0
Stripe version : 11.3.0
OS : Windows 11
Getting this error in API : stripe.customers.create

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