Giter VIP home page Giter VIP logo

Comments (4)

teamon avatar teamon commented on July 29, 2024

I'm not sure I understand your question, but I'll try to answer it anyway :)

You can send a POST request using Tesla.post function (or YourApi.post).
The arguments are: def post(url, payload, options \\ []).
By default the payload is sent as-is. Let me show this using few examples:

Sending raw data:

iex(1)> Tesla.post("http://httpbin.org/post", "my-post-data").body |> IO.puts
{
  "args": {},
  "data": "my-post-data",
  "files": {},
  "form": {},
  "headers": {
    "Content-Length": "12",
    "Content-Type": "",
    "Host": "httpbin.org"
  },
  "json": null,
  "origin": "89.65.176.175",
  "url": "http://httpbin.org/post"
}

Sending form data:

iex(2)> Tesla.post("http://httpbin.org/post", "a=1&b=2", headers: %{"content-type": "application/x-www-form-urlencoded"}).body |> IO.puts
{
  "args": {},
  "data": "",
  "files": {},
  "form": {
    "a": "1",
    "b": "2"
  },
  "headers": {
    "Content-Length": "7",
    "Content-Type": "application/x-www-form-urlencoded",
    "Host": "httpbin.org"
  },
  "json": null,
  "origin": "89.65.176.175",
  "url": "http://httpbin.org/post"
}

Sending JSON:

iex(3)> Tesla.post("http://httpbin.org/post", ~s'{"hello": 123}', headers: %{"content-type": "application/json"}).body |> IO.puts
{
  "args": {},
  "data": "{\"hello\": 123}",
  "files": {},
  "form": {},
  "headers": {
    "Content-Length": "14",
    "Content-Type": "application/json",
    "Host": "httpbin.org"
  },
  "json": {
    "hello": 123
  },
  "origin": "89.65.176.175",
  "url": "http://httpbin.org/post"
}

Tesla does not assume what kind of content you want to send.

Obviously, when building an API client we would like to avoid repetition, like e.g. json encoding the payload. That's what middlewares are for.

defmodule Httpbin do
  use Tesla

  plug Tesla.Middleware.BaseUrl, "http://httpbin.org/"
  plug Tesla.Middleware.JSON
end

and then

iex(4)> Httpbin.post("/post", %{"a" => 1}).body
%{"args" => %{}, "data" => "{\"a\":1}", "files" => %{}, "form" => %{},
  "headers" => %{"Content-Length" => "7", "Content-Type" => "application/json",
    "Host" => "httpbin.org"}, "json" => %{"a" => 1},
  "origin" => "89.65.176.175", "url" => "http://httpbin.org/post"}

The Tesla.Middleware.JSON middleware takes care of both encoding the payload as json, adding correct 'Content-Type' header, as well as decoding the response from JSON.

Please let me know if that answers your question.

from tesla.

anbsky avatar anbsky commented on July 29, 2024

Thank you for your response. To be precise, I mean sending application/x-www-form-urlencoded params in POST requests. HTTPoison does it like this:

HTTPoison.post(url, {:form, [key: "value"]})

I realize now that I can encode it manually, but this just seems like a mundane task (that is already solved in a lot of clients/languages that I'm coming from).

from tesla.

teamon avatar teamon commented on July 29, 2024

Currently there is no FormUrlencoded middleware (simply because I never needed it) but it should be straightforward to add (see json.ex).

Tesla was built with custom API clients in mind. There are not many use cases for direct usage of Tesla.get/post functions - you would rather build a custom module and choose whatever middlewares you need.

from tesla.

anbsky avatar anbsky commented on July 29, 2024

Fair enough. I'll see if I can make this into a pull request. Thanks!

from tesla.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.