0

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

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

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

4

1 回答 1

2

HTTPoison模块具有get发出HTTP GET请求的功能。

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

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"
}
于 2019-04-19T10:09:25.340 回答