How to create HTTPoison.delete with body?

Hello, I need to create HTTPoison.delete with body , but it shows me error like this :

  ** (UndefinedFunctionError) function HTTPoison.delete/4 is undefined or private. Did you mean one of:

      * delete/1
      * delete/2
      * delete/3

my code is :

defp delete_sender_json_without_authorization(params, link) do
    body = Jason.encode!(params)
    HTTPoison.delete(
      "#{@link}#{link}",
       body,
        [
          {"Content-Type", "application/json; charset=UTF-8"},
        ],
       [timeout: 50_000, recv_timeout: 50_000]
    )
  end
end

I searched this and found this link , but he suggested that I should use request method. this is not good for me because I need to use Delete method and send some body with it.

How can I fix this , should I use request method or not ? by the way , my API destination created by DELETE method

Thanks.

The request function does allow you to specify your method as the first argument (:delete in this case), as well as a request body.

HTTPoison.request(:delete,
  "#{@link}#{link}",
  body,
  [
    {"Content-Type", "application/json; charset=UTF-8"},
  ],
  [timeout: 50_000, recv_timeout: 50_000]
)

Given that DELETE indeed may take a body as per HTTP it’s strange HTTPoison doesn’t have a delete/4. Perhaps you should open an issue with them?

4 Likes

I think , there is no way, I am forced to use HTTPoison.request

Yes, if you want to send a DELETE request with a body, you need to use request/5. It seems like you think that’s a problem?

2 Likes

I think why I can’t use delete method for this , if the HTTPoison team added this , it would be better than now

1 Like

Yes, they probably should. Feel free to open an issue with them. In the meantime though, using request/5 with :delete as the first argument will be exactly equivalent.

2 Likes