Sometimes I just need to feed the SEO monster for Erlang. In this case, I could not find a good post that described doing an Erlang http:request that posts with a json payload. This is to support the BDD (“Cucumber but really fast”) testing framework that I’ve included in Crowbar.
Method = post, URL = "http://192,168.124.10:3000/node/2.0/new", Header = [], Type = "application/json", Body = "{\"name\":\"foo.example.com\"}", HTTPOptions = [], Options = [], R = httpc:request(Method, {URL, Header, Type, Body}, HTTPOptions, Options), {ok, {{"HTTP/1.1",ReturnCode, State}, Head, Body}} = R. {"id", Key} = lists:keyfind("id",1,json:parse(Body)),
Note: See the full source: https://github.com/crowbar/barclamp-crowbar/blob/master/BDD/digest_auth.erl
The Body will contain your JSON response and I used our Erlang json lib to parse that to find the ID.
In the actual BDD code, it’s a little more complex because I also use our Erlang digest auth. I hope this helps you!
thanks, very useful.
I think there is a mistake, there should be httpc instead of http
LikeLiked by 1 person
I think so too for the latest erlang libs. This is pretty old and the http client changed.
LikeLike
Thanks again for letting me know. The original source (https://github.com/crowbar/barclamp-crowbar/blob/master/BDD/digest_auth.erl) had been updated and I did not think to go back to this post.
LikeLike
thanks……….
LikeLike
the code as is will result in a badmatch error because you try to use Body for the send and response
LikeLike
Can we send it like real object format ({“name”:”foo.example.com”}) instead stringify (“{\”name\”:\”foo.example.com\”}”) format?
LikeLike