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?
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)
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".
Related
Based on this post and thanks to the #glennsl iam getting some where.
First if someone has a link that i could learn about the parses i will be very glad.
page : Url.Url -> String
page url =
case (Parser.parse (Parser.query (Query.string "name")) url) of
Nothing -> "My query string: " ++ (Maybe.withDefault "empty" url.query)
Just v -> case v of
Just v2 -> "Finnaly a name"
Nothing -> "????"
As far i can understand the expression Parser.parse (Parser.query (Query.string "name")) urlis returning a Maybe (Maybe String) I see this as the parser could return something, and if do it could be an string, is that right?
In my mind if i have the parameter name in my url then my first Just would be executed and then i can get the name.
But no mather what i put on my url it always go the the first Nothing
The result i got
The problem is that you're not parsing the path part of the URL, which is what Url.Parser is primarily for. You have to match the path exactly.
Here's a parser that will match your URL:
s "src" </> s "Main.elm" <?> (Query.string "name")
Note also that parsing the query string is optional, meaning this will also match your URL:
s "src" </> s "Main.elm"
But as long as you include a query param parser, that also has to match.
If all you care about is the query parameter, you'll have to parse the query string specifically, by either writing your own function to do so, or using a library like qs for example:
QS.parse
QS.config
"?a=1&b=x"
== Dict.fromList
[ ( "a", One <| Number 1 )
, ( "b", One <| Str "x" )
]
I am trying to understand elm's type signatures. What does this function return exactly? It appears to be a function that accepts no arguments and returns ...
route : Parser (Page -> a) a
As a learning exercise for myself I'm going to try to answer this. Others will chip in if I get something wrong.
I'm sure you are used to something like
type Person = Adult String | Child String Age
Child is a type that takes two parameters. Parser is the same. But it's definition is pretty formidable
type Parser a b =
Parser (State a -> List (State b))
type alias State value =
{ visited : List String
, unvisited : List String
, params : Dict String String
, value : value
}
That said, you see how Parser is ultimately a wrapper around a function from a State to a list of States. Ultimately it is going to be passed a List of 'unvisited' strings or params; it will progressively 'visit' each one and the result will be combined into the final 'value'.
Next, note that while Parser takes two type parameters - a, b - parseHash is defined
parseHash : Parser (a -> a) a -> Location -> Maybe a
So, your original
route : Parser (Page -> a) a
is going to have to be
route : Parser (Page -> Page) Page
to type check.
To return to your original question, therefore, route is a Parser (which is a very general object) that encapsulates instructions on how to go from one Page to another, and can be used - via parseHash - to tell you what Page to go to next, and that is of course what you would expect from a router.
Hope this gets you started
I am just getting started with eJabberd and am writing a custom module with HTTP access.
I have the request going through, but am now trying to retrieve a custom header and that's where I'm having problems.
I've used the Request record to get the request_headers list and can see that it contains all of the headers I need (although the one I'm after is a binary string on both the key and value for some reason...) as follows:
[
{ 'Content-Length', <<"100">> },
{ <<"X-Custom-Header">>, <<"CustomValue">> },
{ 'Host', <<"127.0.0.1:5280">> },
{ 'Content-Type', <<"application/json">> },
{ 'User-Agent', <<"Fiddler">> }
]
This is also my first foray into functional programming, so from procedural perspective, I would loop through the list and check if the key is the one that I'm looking for and return the value.
To this end, I've created a function as:
find_header(HeaderKey, Headers) ->
lists:foreach(
fun(H) ->
if
H = {HeaderKey, Value} -> H;
true -> false
end
end,
Headers).
With this I get the error:
illegal guard expression
I'm not even sure I'm going about this the right way so am looking for some advice as to how to handle this sort of scenario in Erlang (and possibly in functional languages in general).
Thanks in advance for any help and advice!
PhilHalf
The List that you have mentioned is called a "Property list", which is an ordinary list containing entries in the form of either tuples, whose first elements are keys used for lookup and insertion or atoms, which work as shorthand for tuples {Atom, true}.
To get a value of key, you may do the following:
proplists:get_value(Key,List).
for Example to get the Content Length:
7> List=[{'Content-Length',<<"100">>},
{<<"X-Custom-Header">>,<<"CustomValue">>},
{'Host',<<"127.0.0.1:5280">>},
{'Content-Type',<<"application/json">>},
{'User-Agent',<<"Fiddler">>}].
7> proplists:get_value('Content-Type',List).
<<"application/json">>
You can use the function lists:keyfind/3:
> {_, Value} = lists:keyfind('Content-Length', 1, Headers).
{'Content-Length',<<"100">>}
> Value.
<<"100">>
The 1 in the second argument tells the function what tuple element to compare. If, for example, you wanted to know what key corresponds to a value you already know, you'd use 2 instead:
> {Key, _} = lists:keyfind(<<"100">>, 2, Headers).
{'Content-Length',<<"100">>}
> Key.
'Content-Length'
As for how to implement this in Erlang, you'd write a recursive function.
Imagine that you're looking at the first element of the list, trying to figure out if this is the entry you're looking for. There are three possibilities:
The list is empty, so there is nothing to compare.
The first entry matches. Return it and ignore the rest of the list.
The first entry doesn't match. Therefore, the result of looking for this key in this list is the same as the result of looking for it in the remaining elements: we recurse.
find_header(_HeaderKey, []) ->
not_found;
find_header(HeaderKey, [{HeaderKey, Value} | _Rest]) ->
{ok, Value};
find_header(HeaderKey, [{_Key, _Value} | Rest]) ->
find_header(HeaderKey, Rest).
Hope this helps.
After shamelessly pilfering a code snippet from Tomas Petricek's Blog:
http://tomasp.net/blog/csharp-fsharp-async-intro.aspx
Specifically, this one (and making a few alterations to it):
let downloadPage(url:string) (postData:string) = async {
let request = HttpWebRequest.Create(url)
// Asynchronously get response and dispose it when we're done
use! response = request.AsyncGetResponse()
use stream = response.GetResponseStream()
let temp = new MemoryStream()
let buffer = Array.zeroCreate 4096
// Loop that downloads page into a buffer (could use 'while'
// but recursion is more typical for functional language)
let rec download() = async {
let! count = stream.AsyncRead(buffer, 0, buffer.Length)
do! temp.AsyncWrite(buffer, 0, count)
if count > 0 then return! download() }
// Start the download asynchronously and handle results
do! download()
temp.Seek(0L, SeekOrigin.Begin) |> ignore
let html = (new StreamReader(temp)).ReadToEnd()
return html };;
I tried to do the following with it, and got the error on the last line:
The type was expected to have type Async<'a> but has string -> Asnyc<'a> instead
I googled the error but couldn't find anything that revealed my particular issue.
let postData = "userid=" + userId + "&password=" + password + "&source=" + sourceId + "&version=" + version
let url = postUrlBase + "100/LogIn?" + postData
Async.RunSynchronously (downloadPage(url, postData));;
Also, how would I modify the code so that it downloads a non-ending byte stream (but with occasional pauses between each burst of bytes) asynchronously instead of a string? How would I integrate reading this byte stream as it comes through? I realize this is more than one question, but since they are are all closely related I figured one question would save some time.
Thanks in advance,
Bob
P.S. As I am still new to F# please feel free to make any alterations/suggestions to my code which shows how its done in a more functional style. I'm really trying to get out of my C# mindset, so I appreciate any pointers anyone may wish to share.
Edit: I accidentally pasted in the wrong snippet I was using. I did make an alteration to Tomas' snippet and forgot about it.
When I attempt to run your code downloadPage(url, postData) doesn't work as downloadPage expects two seperate strings. downloadPage url postData is what is expected.
If you changed the let binding to tuple form, or let downloadPage(url:string, postData:string) your call would have worked as well.
To explain why you got the error you got is more complicated. Curried form creates a function that returns a function or string -> string -> Async<string> in your case. The compiler therefore saw you passing a single parameter (tuples are single items after all) and saw that the result would have to be a string -> Async<string> which is not compatible with Async<string>. Another error it could have found (and did in my case) is that string * string is not compatible with string. The exact error being Expected string but found 'a * 'b.
This is what I had:
Async.RunSynchronously (downloadPage(url, postData));;
this is what worked after continued random guessing:
Async.RunSynchronously (downloadPage url postData);;
Although, I'm not sure why this change fixed the problem. Thoughts?
How do you create a link with query string parameters:
/path/to/view?param=358&name=Something+with+spaces
in Lift? I know you can simply write it, I am looking for a wise approach, which encode spaces and other special characters. For example:
Link("path/to/view").param("param", 358).param("name", "Something with spaces")
Thanks in advance,
Etam.
There is appendParams method in net.liftweb.util.HttpHelpers trait:
import net.liftweb._
import util.Helpers._
val url = appendParams("/path/to/view",
("param" -> "358") ::
("name" -> "Something with spaces") :: Nil)
Reply from Scala REPL:
url: String = /path/to/view?param=358&name=Something+with+spaces
As you can see, it gets URL as a String, Seq of param tuples and finally returns String.