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

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -