-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Demonstrate Parameters for HTTP::Client #5145
Conversation
src/http/client.cr
Outdated
# params = HTTP::Params.encode({ "q" => "test"}) # => q=test | ||
# response = HTTP::Client.get "http://www.example.com?" + params | ||
# response.status_code # => 200 | ||
# response.body.lines.first # => "<!doctype html>" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to repeat this from the previous example if it's not relevant.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it'd be a good idea to keep at least one of those output lines to show symmetry with the previous example.
src/http/client.cr
Outdated
# ``` | ||
# require "http/client" | ||
# | ||
# params = HTTP::Params.encode({ "q" => "test"}) # => q=test |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No space in {"
. Run crystal tool format
, that might fix it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is also what caused the CI failure.
src/http/client.cr
Outdated
# require "http/client" | ||
# | ||
# params = HTTP::Params.encode({ "q" => "test"}) # => q=test | ||
# response = HTTP::Client.get "http://www.example.com?" + params |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe worth having a more complex example? 2 parameters, one of which contains something that needs to be escaped? {"author" => "John Doe", "offset" => 20}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, those sound good to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Although I had to change the second value to a string instead of an int. "20" works, but 20 throws an error.
I strongly dislike using this method for creating queries with params. The correct way is to set the params using a |
I like it. It's what I was advised to do when I asked about this use case in the gitter chat. I like that it keeps everything within the HTTP module. I also really like that you can take a hash, and just turn it into a URL encoded string in just one step. Very user friendly. |
Surely just adding a named parameter with the URL params is more friendly than manually appending the URL with |
Can you finally show the better replacement that you're talking about, in an example? |
I don't see any params method in the URI class, so I'm not sure what you're getting at. I agree that an example would help us understand what you're suggesting. |
uri = URI.parse("http://www.example.com")
uri.query = HTTP::Params.encode({ "q" => "test"})
HTTP::Client.get(uri) |
I see how that works, but I don't think it's inherently better. Now granted, that example should be added to the documentation, but I'd place it in the URI class documentation. Quite frankly, having just looked through that documentation, I did not see anything to suggest this as possible. |
Also @RX14 , I love the idea of |
What about: HTTP::Client.get("http://www.example.com?a=b", query_params: {"foo" => "bar"}) Or even: HTTP::Client.get("http://www.example.com?foo=baz", query_params: {"foo" => "bar"}) That means HTTP::Client now has to parse the URI, parse the query parameters and combine them with the given ones. Please, let's not add more and more features for no reason. In fact, can we declare a feature freeze? :-) |
First, can someone tell me why the checks are failing? Not sure what I did wrong. |
@asterite, in that case, I would say that the developer is doing a very bad job by writing some params in the address, and others in the params hash. With proper documentation, any good developer would know to put all their params in the params hash, and NOT in the address string. In any case, it seems like a different discussion than this PR, so I'm game for opening an issue if you guys are. |
|
Just did that, and it changed files that I haven't touched. Is it supposed to do that? |
When I ran src/compiler/crystal/tools/init.cr As well as client.cr. Before I commit and push, can I be sure that it won't break anything? |
Dang... you might have to run the latest formatter on the latest source code. |
Make gave me an error.
|
Well, basically you need to do the proper setup for compiling Crystal, in order to contribute to it. But that's not necessary for now. The tool pointed out the problem with the file you changed. Just ignore the rest and move on. |
Like this? |
@HCLarsen I think the is a good example. |
This is the first activity on this PR in a few months. Do you guys want to merge this? |
This looks good to me. |
@asterite I hope you dont mind me pinging you but I see you are the last one that posted a doc change. Does this look good to you? |
@HCLarsen what is the status on this? |
You're asking me? I fixed what I was told to fix, and yet there's been no movement on this in over a year. For all I know, the changes made to the language made since then have now made my example invalid. |
I would like more docs around anything. |
* Demonstrate Parameters for HTTP::Client * Improve HTTP::Client with Params example * Format client.cr
Adding parameters to an HTTP request seems like a common use case, so some example code would be very useful in the documentation.