Shubham Sharma
Shubham Sharma

Reputation: 195

Unable to make HTTP Post request using elixir?

The code i am trying with is:-

response = HTTPotion.post(url, [body: "{channel: \"#bot\", username: \"watson\", text: \"test\"}"])

The response i am getting is:-

%HTTPotion.Response{body: "invalid_payload",......, status_code: 400}

Upvotes: 0

Views: 802

Answers (1)

tkowal
tkowal

Reputation: 9299

You made a successful request, but the body was wrong. In JSON there should be quotes around the field names:

[body: "{\"channel\": \"#{bot}\", \"username\": \"watson\", \"text\": \"test\"}"]

Also the syntax for string interpolation is #{variable_name} for example:

iex(1)> bot = "mybot"
iex(2)> "#{bot}"

Manually encoding JSON is error prone so you probably want to use Poison.

iex(3)> Poison.encode!(%{bot: bot, username: "watson", text: "test"})
"{\"username\":\"watson\",\"text\":\"test\",\"bot\":\"mybot\"}"

Upvotes: 2

Related Questions