Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting headers in a HTTPC post request in Erlang

I am trying to fire a HTTPC post request from Erlang:

httpc:request(post, {"https://android.googleapis.com/gcm/send",[{"Authorization","key=api_key_generated_at_googleaccount"}],[{"Content-Type","application/json"}],Body},[],[])

But every time I fire this request my Erlang shell hangs. Is there any problem in the request?

like image 829
Geek Avatar asked Feb 03 '15 06:02

Geek


1 Answers

I think you mixed up the arguments to http:request/4 a bit, in the manual for httpc:

request(Method, Request, HTTPOptions, Options)

   Request          = request()
   request()        = {url(), headers()} |
                      {url(), headers(), content_type(), body()}
   content_type()   = string()

Above you can see content_type() should be a string() and not [{string(),string()}].

It is easier if to read if you put the whole request() into a variable:

10> inets:start().
ok
11> ssl:start().
ok
12> Body = "some cool json",
12> Request = {"https://android.googleapis.com/gcm/send", [{"Authorization","key=blabla"}], "application/json", Body},
12> httpc:request(post, Request, [], []).
{ok,{{"HTTP/1.1",401,"Unauthorized"},
     [{"cache-control","private, max-age=0"},
      {"date","Tue, 03 Feb 2015 07:19:07 GMT"},
      {"accept-ranges","none"},
      {"server","GSE"},
      {"vary","Accept-Encoding"},
      {"content-length","147"},
      {"content-type","text/html; charset=UTF-8"},
      {"expires","Tue, 03 Feb 2015 07:19:07 GMT"},
      {"x-content-type-options","nosniff"},
      {"x-frame-options","SAMEORIGIN"},
      {"x-xss-protection","1; mode=block"},
      {"alternate-protocol","443:quic,p=0.02"}],
     "<HTML>\n<HEAD>\n<TITLE>Unauthorized</TITLE>\n</HEAD>\n<BODY BGCOLOR=\"#FFFFFF\" TEXT=\"#000000\">\n<H1>Unauthorized</H1>\n<H2>Error 401</H2>\n</BODY>\n</HTML>\n"}}
like image 182
emil Avatar answered Oct 20 '22 17:10

emil