HTTPoison request fails

Hi guys im trying to use HTTPoison to make the following request
In the Postman app, complete the following:

  1. Set the verb to POST.
  2. Enter https://api-m.sandbox.paypal.com/v1/oauth2/token as the request URL.
  3. Select the Authorization tab.
  4. From the TYPE list, select Basic Auth.
  5. In the Username field, enter your client ID.
  6. In the Password field, enter your secret.
  7. Select the Body tab.
  8. Select the x-www-form-urlencoded option.
  9. In the KEY field, enter grant_type.
  10. In the VALUE field, enter client_credentials.
  11. Select Send.

It works fine in postmanI need help to make request in HTTPoison should be thankful for help

hey

    credentials = "username:password" |> Base.encode64()

    header = [
      {"Content-Type", "x-www-form-urlencoded"},
      {"Authorization", "Basic #{credentials}"}
    ]

    body = %{"grant_type" => "client_credentials"}

    req = HTTPoison.post!("https://api-m.sandbox.paypal.com/v1/oauth2/token", Jason.encode!(body), header)
1 Like

I have tried this but its returning this

`%HTTPoison.Response{
  body: "{\"name\":\"Bad Request\",\"debug_id\":\"c56c9c74dd210\",\"message\":\"java.lang.IllegalArgumentException\",\"details\":[]}",
  headers: [
    {"Connection", "keep-alive"},
    {"Content-Length", "109"},
    {"Content-Type", "application/json"},
    {"Server", "nginx/1.18.0 (Ubuntu)"},
    {"Cache-Control", "max-age=0, no-cache, no-store, must-revalidate"},
    {"Paypal-Debug-Id", "c56c9c74dd210"},
    {"Strict-Transport-Security", "max-age=31536000; includeSubDomains"},
    {"Edge-Control", "max-age=0"},
    {"Accept-Ranges", "bytes"},
    {"Date", "Sat, 28 May 2022 20:00:40 GMT"},
    {"Via", "1.1 varnish"},
    {"X-Served-By", "cache-lhr7375-LHR"},
    {"X-Cache", "MISS"},
    {"X-Cache-Hits", "0"},
    {"X-Timer", "S1653768040.137049,VS0,VE373"}
  ],
  request: %HTTPoison.Request{
    body: "{\"grant_type\":\"client_credentials\"}",
    headers: [
      {"Content-Type", "x-www-form-urlencoded"},
      {"Authorization",
       "Basic QVdHNEpyQ2twTW1QOFROeXROY1FxbmpMOWN4bmNmY2diMmtpX2NPTWtfTEZGbVZCdVVDUU5fUFZabDhqRU86RUNzYVNqZ2t"}
    ],
    method: :post,
    options: [],
    params: %{},
    url: "https://api-m.sandbox.paypal.com/v1/oauth2/token"
  },
  request_url: "https://api-m.sandbox.paypal.com/v1/oauth2/token",
  status_code: 400
}
[info] Sent 200 in 1282ms
^[[2A
`

The way around I’m using now is

`{res,_}=System.cmd "curl", ["POST", "-H", "Accept: application/json","-H", "Accept-Language: en_US", "https://api-m.sandbox.paypal.com/v1/oauth2/token", "-d", "grant_type=client_credentials","-u", "username:password"]
    token_map=
    res |> String.replace("'", "\"") |> Poison.decode!()`
credentials = "username:password" |> Base.encode64()

header = [
  {"Accept", "application/json"},
  {"Accept-Language", "en_US"},
  {"Content-Type", "x-www-form-urlencoded"},
  {"Authorization", "Basic #{credentials}"}
]

req = HTTPoison.post!("https://api-m.sandbox.paypal.com/v1/oauth2/token", "grant_type=client_credentials", header)
3 Likes

Thanks it worked