Dayron icon indicating copy to clipboard operation
Dayron copied to clipboard

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

Open samuelroy opened this issue 8 years ago • 3 comments

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!

samuelroy avatar Dec 03 '16 14:12 samuelroy

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

tiagoengel avatar Dec 04 '16 13:12 tiagoengel

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)!

samuelroy avatar Dec 04 '16 14:12 samuelroy

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

tiagoengel avatar Dec 04 '16 15:12 tiagoengel