The Crystal Programming Language Forum

Equivalent of a Curl request

I need help with find the equivalent of the following curl request in Crystal. Would be great if it could be done with the Halite library (because it is really great and easy to use other than the issue I am having here), but another solution would be great too.

This is the example curl request on the Stripe page (here):

curl https://api.stripe.com/v1/checkout/sessions \
  -u sk_test_4eC39HqLyjWDarjtT1zdp7dc: \
  -d payment_method_types[]=card \
  -d line_items[][name]=T-shirt \
  -d line_items[][description]="Comfortable cotton t-shirt" \
  -d line_items[][images][]="https://example.com/t-shirt.png" \
  -d line_items[][amount]=500 \
  -d line_items[][currency]=usd \
  -d line_items[][quantity]=1 \
  -d success_url="https://example.com/success" \
  -d cancel_url="https://example.com/cancel"

I tried the following using the Halite library:

resp = Halite.auth("Bearer sk_test_Apikeyhere")
      .post("https://api.stripe.com/v1/checkout/sessions", form: {
          "payment_method_types[]" => ["card"],
          "line_items[]" => [{"name" => "awesomeitem", 
                           "description" => "short description",
                           "amount" => 699, 
                           "currency" => "usd", 
                           "quantity" => 1}],
          "success_url" => "http://localhost:3000",
          "cancel_url" => "https://localhost:3000"
      })

I get the following error in response:

    {
      "error": {
        "message": "Invalid hash",
        "param": "line_items[0]",
        "type": "invalid_request_error"
      }
    }

The line_items parameter help on Stripe is here:

https://stripe.com/docs/api/checkout/sessions/create#create_checkout_session-line_items

Thank you for your help.

You could try https://github.com/vladfaust/stripe.cr, as it abstracts away raw HTTP requests. It’s also easy to extend with missing endpoints.

Thank you @vladfaust. I looked at it before asking here. It does not support the checkout sessions (https://stripe.com/docs/payments/checkout/server#integrate), does it?

No, not yet. But you can easily add them yourself into the shard…

Thank you. Will look at your source to see if I can figure it out.

Also, you can just pass those HTTP parameters using any http client. Just don’t expect clients to transform array and hashes to the (non-standard) format Strip uses. What I mean is, using your original curl request, pass those parameters exactly like that in any http client (even HTTP::Client from the standard library).

1 Like

Thank you. I will try this.

@asterite,
Thank you so much. Making a raw request using Halite works well. Where were you yesterday?? (Just kidding!) Appreciate your response and thank you again for this suggestion.

have you seen https://github.com/mamantoha/crest/blob/e91e83bd2d56328cd7b547299222876c0fe209c0/src/crest/curlify.cr

@wontruefree. Thanks for the link. I had not seen this. But I think solution by @asterite works great, and is probably more straightforward. Although, this could be useful in another instance.

1 Like