如何将 http 请求发送到 Stripe 而不是 curl 请求



我可以成功向 Stripe API 发送 curl 请求,就像这样:

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

如何使用Elixir依赖项:HTTPoison来发送HTTP请求?

HTTPoison模块具有发出HTTP GET请求的get函数。

您可以在iex中运行h HTTPoison.get以获取有用的帮助消息。

最好在

加载库的情况下启动iex,并使用各种值探索所需的函数,并检查返回或生成的数据。

在这种情况下,这样的事情应该让你开始:

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

.

Poison.decode!(body)部分将是这样的

%{
  "data" => [
    %{
      "account_balance" => 0,
      "address" => nil,
      "created" => 1555667607,
      "currency" => nil,
      "default_source" => "card_xxxxxxxxxx",
      "delinquent" => false,
      "description" => nil,
      "discount" => nil,
      "email" => "xxx@exemple.com",
      "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"
}

最新更新