1

I'm trying to send parameters with an URL, like http://localhost:3000/register?name=Chris&job=typist. I can send that all at once as a string with httpc:request, but I can't find a function to put the query parameters in the URL(given a dictionary).

Is there another HTTP library I should be using that has this capability?

I'd like to give it a root URL with a hash/dictonary/map (in json {"a" : "b", "c" : "d"}) that then appends it correctly to the end of the url. For example, given "www.facebook.com" and [{"a", "b"}, {"c", "d"}] would give "www.facebook.com?a=b&c=d".

Here is a similar question for Ruby: Ruby: How to turn a hash into HTTP parameters?

Community
  • 1
  • 1
Chris
  • 11,819
  • 19
  • 91
  • 145

2 Answers2

0

I'm not sure exactly what you mean by "hash", but if you'd like to construct a query string from tuples, that is a fairly straitforward task.

I'm not familiar with a method in httpc to provide the functionality you desire. You can write a wrapper around request/4 very easily, similar to this.

(This program is hastily constructed to give you the idea, forgive any errors).

request(Method, Domain, {Path, Query, Fragment}, HTTPOptions, Options) -> 
    QueryString = lists:flatten([Path,
                   case Query of "" -> ""; _ -> [$? | Query] end,
                   case Fragment of "" -> ""; _ -> [$# | Fragment] end]);
    Request = concat(Domain, QueryString);
    httpc:request(Method, {Request, []}, HTTPOptions, Options).

You can invoke it like

request(get, "http://www.example.com", {"/path", "", "bar?baz}, HTTPOptions, Options)

zetavolt
  • 2,989
  • 1
  • 23
  • 33
  • I'd like to give it a root URL with a hash/dictonary/map (in json {"a" : "b", "c" : "d"}) that then appends it correctly to the end of the url. => "www.facebook.com?a=b&c=d" – Chris Sep 03 '12 at 22:47
  • Erlang's string processing capabilities may be lacking, but I think you could write a method to do that quite easily. If you can't figure it out, consult a library like mochijson2. – zetavolt Sep 03 '12 at 23:43
0

try this function

test(URL,QP)->URL++"?"++loop(QP,[]).

loop([{A,B}],QP)->QP++A++"="++B;
loop([{A,B}|T],QP)->loop(T,QP++A++"="++B++"&").

call test("www.facebook.com",[{"a", "b"}, {"c", "d"}]). it returns "www.facebook.com?a=b&c=d".

IMHeadHunter
  • 272
  • 3
  • 11