go - Buffer issue cannot use <type> as type io.Reader in argument to http.NewRequest golang -
i having issue when passing string in http.newrequest in golang body param.
the error got :
cannot use req.body (type string) type io.reader in argument http.newrequest: string not implement io.reader (missing read method)
similarly there other use cases buffer required input instead of particular type or array of it. eg, byte[] when input required buffer.
what did error mean , ways of solving , understanding golang trying enforce.
edit: line having issue , did not find references.
http.newrequest(req.method, req.url, req.body)
http.newrequest(req.method, req.url, strings.newreader(req.body)) solves issue. planning add answer (as fyi type of question)
this error means http.newrequest
method take io.reader
interface body
argument:
func newrequest(method, urlstr string, body io.reader) (*request, error)
it done way can pass file, connection server, response else request easily.
the "problem" string
doesn't implement io.reader
interface, defined follow:
type reader interface { read(p []byte) (n int, err error) }
its not huge problem, can use strings.reader
type wrapper around string
implement said interface.
func newreader(s string) *reader
tip: there bytes.reader
type times have []byte
pass parameter.
Comments
Post a Comment