Re: [go-nuts] sending server response to a client by w responseWriter

2021-10-07 Thread 'Axel Wagner' via golang-nuts
On Thu, Oct 7, 2021 at 1:43 PM RiSi wrote: > if error happens by io.Copy, can I set the status Code of w after io.Copy? > No. io.Copy might have already written parts of the body. You can't set the status after parts of the body have been written. > > > Am Do., 7. Okt. 2021 um 13:41 Uhr schri

Re: [go-nuts] sending server response to a client by w responseWriter

2021-10-07 Thread RiSi
if error happens by io.Copy, can I set the status Code of w after io.Copy? Am Do., 7. Okt. 2021 um 13:41 Uhr schrieb RiSi : > > *w.Header().Set("Content-Type", "application/xml")* > > *_, err:=io.Copy(w,src) * > > *if err!=nil{* > *re := &res{ Message: "Error happened",}* > > > > *w.Header().Set

Re: [go-nuts] sending server response to a client by w responseWriter

2021-10-07 Thread RiSi
*w.Header().Set("Content-Type", "application/xml")* *_, err:=io.Copy(w,src) * *if err!=nil{* *re := &res{ Message: "Error happened",}* *w.Header().Set("Content-Type", "application/json")w.WriteHeader(599) //sample status codeerr = json.NewEncoder(w).Encode(re)* *}//if* *the part after err!=ni

Re: [go-nuts] sending server response to a client by w responseWriter

2021-10-07 Thread 'Axel Wagner' via golang-nuts
As I said in your last thread: https://groups.google.com/g/golang-nuts/c/I5gwXr1kOA0/m/oIVYRgBoAQAJ What you want is impossible, based on how HTTP works. The status of a request must be determined *before* any of the body is written. So, if writing the body fails, there is nothing more you can do -

[go-nuts] sending server response to a client by w responseWriter

2021-10-07 Thread RS
Best practice in following scenario: In my func for an endpoint (handler) with func hello(w http.ResponseWriter, req *http.Request) I call another server get the resp.Body (Body io.ReadCloser) and want to write this Body in w (ResponseWriter ). What is best practice? I have already copied dir