pengfei xu
pengfei xu

Reputation: 29

How to send http request to Stripe instead of curl request?

I can send curl request to Stripe API successfully, just like this:

curl https://api.stripe.com/v1/customers?limit=3 \
  -u sk_test_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx: \
  -G

How can I do with Elixir dependency: HTTPoison, to send an HTTP request?

Upvotes: 0

Views: 342

Answers (1)

S.B
S.B

Reputation: 847

HTTPoison module has a get function to make HTTP GET requests.

You can run h HTTPoison.get in your iex to get a useful help message.

Its a good practice to start up iex with your libraries loaded and explore the functions you need with various values and inspect the data that is returned or generated.

In this case, something like this should get you started:

url = "https://api.stripe.com/v1/customers?limit=3"
headers = [ {"Authorization", "Bearer " <> "sk_test_xxxxxxxxxxxxxxxxxxx"} ]

case HTTPoison.get(url, headers, []) do
  {:ok, %HTTPoison.Response{body: body, status_code: 200}} -> 
      # some data is returned
      "success body = " <> Poison.decode!(body)
  {:ok, %HTTPoison.Response{status_code: other_status_code}} -> 
      # api was not able to process our request, check body for details
      "some error returned from endpoint"
  {:error, reason} -> 
      "problem with network or reaching/getting endpoint"
end

.

The Poison.decode!(body) part will be somthing like this

%{
  "data" => [
    %{
      "account_balance" => 0,
      "address" => nil,
      "created" => 1555667607,
      "currency" => nil,
      "default_source" => "card_xxxxxxxxxx",
      "delinquent" => false,
      "description" => nil,
      "discount" => nil,
      "email" => "[email protected]",
      "id" => "cus_xxxxxxxxx",
      "invoice_prefix" => "ASDSADASD",
      "invoice_settings" => %{
        "custom_fields" => nil,
        "default_payment_method" => nil,
        "footer" => nil
      },
      "livemode" => false,
      "metadata" => %{},
      "name" => nil,
      "object" => "customer",
      "phone" => nil,
      "preferred_locales" => [],
      "shipping" => nil,
      "sources" => %{
        "data" => [
          %{
            "address_city" => nil,
            "address_country" => nil,
            "address_line1" => nil,
            "address_line1_check" => nil,
            "address_line2" => nil,
            "address_state" => nil,
            "address_zip" => nil,
            "address_zip_check" => nil,
            "brand" => "Visa",
            "country" => "US",
            "customer" => "cus_EuirEmfjcPKg4Q",
            "cvc_check" => nil,
            "dynamic_last4" => nil,
            "exp_month" => 4,
            "exp_year" => 2020,
            "fingerprint" => "XXXXXXc",
            "funding" => "credit",
            "id" => "card_123123XXX",
            "last4" => "4242",
            "metadata" => %{},
            "name" => nil,
            "object" => "card",
            "tokenization_method" => nil
          }
        ],
        "has_more" => false,
        "object" => "list",
        "total_count" => 1,
        "url" => "/v1/customers/cus_EEEEASDSADAS/sources"
      },
      "subscriptions" => %{
        "data" => [],
        "has_more" => false,
        "object" => "list",
        "total_count" => 0,
        "url" => "/v1/customers/cus_EERASDASD/subscriptions"
      },
      "tax_info" => nil,
      "tax_info_verification" => nil
    },

    ------ more objects in array here, removed for brewity -------

  ],
  "has_more" => true,
  "object" => "list",
  "url" => "/v1/customers"
}

Upvotes: 2

Related Questions