inaka / Dayron

A repository `similar` to Ecto.Repo that maps to an underlying http client, sending requests to an external rest api instead of a database
http://inaka.net/blog/2016/05/24/introducing-dayron/
Apache License 2.0
159 stars 20 forks source link

Get request encode empty body to "" - Throw 403 forbidden response from some API #61

Open samuelroy opened 7 years ago

samuelroy commented 7 years ago

Hi,

I encountered an issue while using your lib to consume API.ai's api. A GET request is actually sending double quotes in body. After hours of debugging (I thought something was wrong with the headers sent), I found that we encode an empty body with Poison.encode even for a GET request.

This behavior causes API.ai to throw a 403 forbidden response.

I'm doing my first step in Elixir and functional programming, so I lack deep knowledge in how to solve in good manner this issue. Below is my attempt:

adapters/httpoison_adapter.ex, line ~24:

     def process_request_body(""), do: "" #tried to return nil but it's not catched by hackney library

     def process_request_body(body), do: Poison.encode!(body)

You can check this behavior using http://requestb.in/ and looking at the raw body of your GET request. it should be empty, it's not.

Looking for your advice and a proper fix!

tiagoengel commented 7 years ago

You are in the right track, in fact I think that your fix should work. I did a request to http://requestb.in/ as you suggested and the request body was empty after aplying your fix.

Are you sure you're testing with the version that have your fix? I've had some problems with this in the past, try running mix deps.clean dayron && mix deps.compile dayron

samuelroy commented 7 years ago

Yes sorry, I wasn't clear in my request! My fix is working, I just didn't know if it was the right/best way to handle this case (with the function and the pattern matching on the double quotes)!

tiagoengel commented 7 years ago

Ohh I see :). Yes, this is the elixir/erlang way, your fix is perfectly fine.