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

时间:2019-04-19 08:40:57

标签: elixir httpoison

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

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

如何处理Elixir依赖项:HTTPoison,以发送HTTP请求?

1 个答案:

答案 0 :(得分: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"
}
相关问题