I'm trying to make post request using tesla but getting error:
(CaseClauseError) no case clause matching: %{name: "ecdebit"}
(hackney) /deps/hackney/src/hackney_request.erl:312::hackney_request.handle_body/4
(hackney) /deps/hackney/src/hackney_request.erl:81::hackney_request.perform/2
(hackney) /deps/hackney/src/hackney.erl:372::hackney.send_request/2
(tesla) lib/tesla/adapter/hackney.ex:69: Tesla.Adapter.Hackney.request/5
(tesla) lib/tesla/adapter/hackney.ex:31: Tesla.Adapter.Hackney.call/2
my request code is
request_body = %{
name: "ecdebit",
}
Tesla.post(client, "/contactdb/lists", request_body)
in tesla base url is: https://api.sendgrid.com/v3
and also set authorization key
. how can we pass data for post request ?
as in tesla documentation define post request as this:
Tesla.post("http://httpbin.org/post", "data", headers: [{"content-type", "application/json"}])
Is there any in this planet who could help to get rid of this glitch :(.
Your quote from the docs about
Tesla.post()
:shows that the first argument is a string containing a url, and the second argument is a string containing the data, and the third argument is a keyword list. Yet, when you call
Tesla.post()
you pass a path as the second argument, and an Elixir map as the third argument:Here is how you can take advantage of Tesla's features to incrementally convert your request to what you want:
1) Create a mix project:
2) Add jason, hackney, and tesla as dependencies in the
mix.ex
file:3) Change
lib/http.ex
to the following:When you specify
Tesla.Middleware.BaseUrl
, Tesla will automatically prepend the base url to the string you specify as the first argument in thepost()
function (unless the string starts with "http" or "https").When you specify
Tesla.Middleware.FormUrlencoded
, Tesla will automatically convert an Elixir map given as the second argument to thepost()
function into the format that a form on a web page sends to a server, which simply looks like this:Tesla also automatically adds the following header to the request:
If you want Tesla to convert the Elixir map into a json string, then specify
Tesla.Middleware.JSON
instead (note this requiresjason
in your dependencies). Tesla will also automatically add the following header to the request:When you specify
Tesla.Middleware.Headers
, Tesla will automatically add the specified headers to all your requests.Now try it out in iex:
The formatted output is the response. At the website
webhook.site
, I can see the request (click on the image to enlarge it):Tesla has two middleware modules for authorization: