BoZhenka

BoZhenka

How send GET-request with a body?

Hello there,
Here I have this request in curl:

curl
-H "AUTHENTICATION: HMAC GPLDIS8H7N5KY8DG:c6881c8bbcee2daceb5d8c2bb54078702bca23d828d7468afeb2f6ed0f25366f:1549890568"
-X GET
-d '{"customer_id":"Chrome","customer_ip":"127.0.0.1"}' 'https://testrest.vload.expert/voucher/validate/2698937783822253/Chrome/11234576536'

This is bad practice with using body in GET-request, unfortunately it is not my choice. So, how can I send it using some Elixir/Erlang library?
I was trying do it by using HTTPoison, HTTPotion in Elixir. And httpc in erlang, but httpc even don’t send GET-request if I put body to it:
:httpc.request(:get, {url, headers, 'text/plain', 'body'}, [], [])

Marked As Solved

lyo5ha

lyo5ha

As far as HTTPoison builds on hackney, it’s possible to handle such unconventional request. It’s not documented well on HTTPoison page, but works :slight_smile:.

iex(11)> request2 = %HTTPoison.Request{                                                                                                                            
...(11)>             method: :get,                                                                                                                                 
...(11)>             headers: [{"AUTHENTICATION", "HMAC GPLDIS8H7N5KY8DG:c6881c8bbcee2daceb5d8c2bb54078702bca23d828d7468afeb2f6ed0f25366f:1549890568"}],                      
...(11)>             body: "{\"customer_id\":\"Chrome\",\"customer_ip\":\"127.0.0.1\"}",                                                                           
...(11)>             url: "https://testrest.vload.expert/voucher/validate/2698937783822253/Chrome/11234576536"                                                     
...(11)> }
iex(12)> HTTPoison.request(request2)

Also Liked

NobbZ

NobbZ

BTDT, but even worse… The underlying server expected some headers in exact order, no more, no less…

Still, we were able to convince the API provider to change this behaviour, as we consumed the API via mobile networks which again were force proxies and the proxies of course touched the headers.

So telling the provider about problems might help them to see issues they didn’t know to exist.

sribe

sribe

Oh absolutely, I had to deal with one with case-sensitive headers, which presents some issues with Elixir. (Tests, IIRC, check case and want all lower…)

Funny thing is, we owned the service in question, and were afraid to even try to touch the code. In fact, that’s one extremely minor example of why the Elixir program in question came into existence.

But yeah, just to reiterate: do point it out to the devs, they may well want to fix it.

sribe

sribe

“Not allowed to” meet “real world”, “real world” meet “not allowed to” :wink:

I agree, obviously, that it is a HORRIBLE design for an API, in fact it is just not HTTP. And I would definitely point that out to the API designers, especially the part about proxies dropping it.

But ultimately, sometimes, we have to use non-compliant APIs.

khaskelberg

khaskelberg

This RFC is now obsolete. It was replaced by RFC 7230-7237. And this RFC allows to send body with GET request.

Request message
framing is independent of method semantics, even if the method does
not define any use for a message body

A payload within a GET request message has no defined semantics;
sending a payload body on a GET request might cause some existing
implementations to reject the request.

It is still not a good idea, but it doesn’t contradict HTTP/1.1 spec

NobbZ

NobbZ

I should take a closer look at those obseletion markers at the top :wink:

Neither does the fact that some clients do not allow to create such a request.

Anyway, there are clients available in elixir that allow GET with body as pointed out earlier. It’s not necessarily convenient to do so :wink:

And still, third party issues exist. A proxy dropping the request does not act against the spec either and still makes it impossible to use the service. This is something that could cost the API provider money and they should be made aware of the issue.

Where Next?

Popular in Questions Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New

Other popular topics Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

We're in Beta

About us Mission Statement