Skip to content
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

Possible improvements for http api #436

Open
marklagendijk opened this issue Dec 21, 2017 · 3 comments
Open

Possible improvements for http api #436

marklagendijk opened this issue Dec 21, 2017 · 3 comments
Labels
new-http issues that would require (or benefit from) a new HTTP API ux

Comments

@marklagendijk
Copy link
Contributor

marklagendijk commented Dec 21, 2017

The current http api is not yet very user friendly. For example: if you want to post JSON data, you manually need to do the serialization and set the Content-Type header.
I think this api would be greatly improved, if it would implement certain concepts from the Node.js request library.

  • Because of method signature limitations of Go (no default params etc), I would make all the methods (get, post, request etc) take a single params object as parameter. E.g. http.request({ method: 'GET', url: 'http://test.com', ... })
  • baseUrl
  • qs - object containing querystring values to be appended to the url
  • body - entity body for PATCH, POST and PUT requests.
  • form - when passed an object or a querystring, this sets body to a querystring representation of value, and adds Content-type: application/x-www-form-urlencoded header.
  • json - sets body to JSON representation of value and adds Content-type: application/json header.
  • defaults method - creates a new instance of the HTTP API with defaults configured. Any param can be set to a default value.

There are more things that could be added, but I consider these as the most important. It is important to be able to easily do JSON and form requests.
We should try to prevent that people need to write utilities to do such basic things themselves.

@liclac
Copy link
Contributor

liclac commented Dec 21, 2017

I like the idea of taking a page from their book. We could easily change it so that url can be either a URL or an object.

@robingustafsson
Copy link
Member

I like this as well, but I'm not sure if auto parsing the response as JSON if json is set is necessary. I mean, isn't this covered by the current Response API with the json() method (as I guess the alternative would be to return the same when body is accessed or json() are called)?

@marklagendijk
Copy link
Contributor Author

marklagendijk commented Jan 5, 2018

@robingustafsson good point. When I wrote this I hadn't yet found out about response.html() and response.json(). I think we should add some examples about those (the OO practice of having methods on returned data, is less common in the Javascript world).
I removed that part from the proposal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new-http issues that would require (or benefit from) a new HTTP API ux
Projects
None yet
Development

No branches or pull requests

4 participants