Consuming ASP.NET C# EXTERNAL API

How to properly consume an C# APi?

So far i try this:

iex(18)> url = "http://172.1.1.1/API/api/login"                                                          "http://172.16.20.143/FileServerAPI/api/login"
iex(19)> data = %{username: "testuser", password: "testpass"}                                                          %{password: "testpass", username: "testuser"}
iex(20)> postdata = URI.encode_query(data)
"password=testpass&username=testuser"
iex(21)> headers = [{"Content-Type", "application/json"}]                
[{"Content-Type", "application/json"}]

But I got this error.

{:ok,
 %HTTPoison.Response{body: "{\"Message\":\"The request contains an entity body but no Content-Type header. The inferred media type 'application/octet-stream' is not supported for this resource.\"}",
  headers: [{"Cache-Control", "no-cache"}, {"Pragma", "no-cache"},
   {"Content-Type", "application/json; charset=utf-8"}, {"Expires", "-1"},
   {"Server", "Microsoft-IIS/8.5"}, {"X-AspNet-Version", "4.0.30319"},
   {"X-Powered-By", "ASP.NET"}, {"Date", "Thu, 16 Mar 2017 07:06:15 GMT"},
   {"Content-Length", "164"}], status_code: 415}}

Using postman, its working fine

Any Idea?

2 Likes

In your mix.lock file what version of hackney is specified? 1.6.6 is retired, if you see that be sure to upgrade.

3 Likes

Also just as heads up, if you start seeing :closed connection results this thread will help you 18.3.4 + Erlang TLS1.2 [+ IIS] Issues

I got really bamboozled with this issue when trying to integrate with the MS graph API/oauth.

2 Likes

My hackney version is 1.6.6. Thank you very much.

1 Like