Comments (4)
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.
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.
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.
Fair enough. I'll see if I can make this into a pull request. Thanks!
from tesla.
Related Issues (20)
- Fuck you HOT 2
- G
- Document the need for telemetry in the mix file HOT 3
- Mint proxy credentials HOT 1
- Issues Working with QuotaGuard QGTunnel HOT 3
- [Proposal] Lower the usage of macros HOT 2
- Setting retry delay on runtime. HOT 1
- Telemetry metadata doesn't have data about response in case of error
- Dialyzer failing on Tesla (master) HOT 3
- Tesla Response Stream is not compatible with Tesla Multipart upload
- Jason.Encoder protocol must always be explicitly implemented HOT 1
- Be more explict on `:no_scheme` error reason HOT 1
- When setting the adapter on runtime, mocking does not work
- Logger debug: true does nothing at runtime HOT 2
- Compiler warnings with Elixir v1.17.0-rc.0 HOT 3
- Tesla.Multipart.assert_part_value! doesn't allow valid stream
- Content type should be case insensitive HOT 2
- Tesla.Mock.pdict_get/0 doesn't work inside of Broadway HOT 3
- Test failures and compiler warnings in Elixir 1.16 (httparrot) HOT 6
- Tesla 1.9.0+ is not compatible with Elixir 1.10 HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tesla.