Comments (7)
Here's one way to enable an io.Writer interface access to the request body using the Do method described elsewhere.
-
The client copies the body io.Reader to the connection using a custom version of io.Copy that prefers io.WriterTo over io.ReaderFrom.
-
Add function
WriteToWrapper(writeTo func(io.Writer) error) io.Reader
If WriteTo is called on the returned reader, then returned object calls the writeTo function do the work. If Read is called on the returned object, then the object creates a pipe and runs the writeTo function in a goroutine writing to the pipe.
-
Applications call Do(method, url, header, WriteToWrapper(func(w io.Writer) error { /* complex request generation code here */}))
from http.
I agree that an io.Writer for the request body could be the right approach. For the simple API it even makes sense:
func Get(dst io.Writer, srcURL string) (n int64, err error)
This would be familiar to anyone who's used io.Copy, and the burden of closing the response body is still with the library and not the user.
from http.
Thank you for your suggestion.
You would have seen the
http.Get(url string)([]byte, error)
Helper that exists now. As a high level method it has a lot of compromises and so I'm open to suggestions to improve it.
On question with your alternate Get function, what happens if the status is !200 ? Many status code can contain bodies, and as the status code is hidden from the caller, what should be done to avoid returning the text of a 404 page as a body?
Maybe at the highest level non 200 status codes should be converted to errors ?
On 27/07/2013, at 16:06, James Gray [email protected] wrote:
I agree that an io.Writer for the request body could be the right approach. For the simple API it even makes sense:
func Get(dst io.Writer, srcURL string) (n int64, err error)
This would be familiar to anyone who's used io.Copy, and the burden of closing the response body is still with the library and not the user.—
Reply to this email directly or view it on GitHub.
from http.
The current Get
function is still conceptually simpler, so I'm not sure if it should be replaced outright by an io.Writer variant. Perhaps it could exist as an alternative named something like GetTo
.
I do think with an API this simple it makes sense to error on anything but a status of 200, but I may just not be thinking of an important case.
from http.
Just a suggestion. Comments and suggestions welcome. Also, if someone wants to write this code
type StatusError Status
func (s *StatusError) Error() string
which lets us convert a status code into an error, it would be much appreciated. This code should be in the http package.
from http.
Reopening because I didn't address Gary's request, only James' suggestion for http.Get.
from http.
io.Copy was changed to prefer io.WriterTo over io.ReaderFrom in tip. The WriteToWrapper trick should work everywhere that io.Copy is used including the net/http client.
from http.
Related Issues (20)
- SendRequest should buffer sending headers
- Better support for controlling accept-encoding
- support 301/302 redirects transparently HOT 1
- Add ability to convert a status code into an error. HOT 5
- Support http authentication HOT 1
- Chunked []byte requests produce incorrect request, hang handler
- Example doesn't work with https://api.github.com HOT 1
- DefaultClient not support cookies
- Unable to create own client - dialer is not exported and no function to set
- Enable Sourcegraph HOT 2
- golang.org/x/net/context support? HOT 2
- It's not support https, and it's better work as same as the golang standard http lib.
- Return body on HTTP error HOT 1
- does GET request support Proxy? HOT 4
- Retry support? HOT 1
- Conditional GET convenience method
- Support gzip compressed response bodies
- Support https:// HOT 1
- Support connection reuse
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from http.