I have a service that collects and processes data (more precisely road traffic data in Datex2) from different governmental organizations. I want to send a 100-continue with a Go http server to a client that sends a request with this header:
Content-Length: "4914"]
Content-Type: "application/xml"
Expect: "100-continue"
The client doesn't send authorization credentials with this request. It waits for the 100 to send a second request, which will contain the auth credentials.
I don't want to read the body of the first request, which would let to sending 100, because I first want to get the authorization credentials.
Here is one example of the codes i tried to use:
if c.Request.Header.Get("Expect") != "" {
c.Writer.WriteHeader(http.StatusContinue)
c.Writer.WriteHeaderNow()
time.Sleep(30 * time.Second)
return
}
In my understanding this should send the 100 immediately. But I sends a response (200) when return is reached. With reference to this answer, I send requests with different informations in the header, but nothing worked out..
Edit: Here are sample client:
func main() {
client := http.Client{}
reqBody := []byte(`{"somekey":"somevalue"}`)
req, err := http.NewRequest(http.MethodPost, "http://localhost:8080/somepath", bytes.NewBuffer(reqBody))
if err != nil {
panic(err)
}
req.Header.Set("Expect", "100-continue")
resp, err := client.Do(req)
if err != nil {
panic(err)
}
if resp.StatusCode != http.StatusContinue {
panic("invalid status code")
} else {
println("ok")
}
}
and server:
func main() {
server := http.Server{
Addr: "localhost:8080",
Handler: http.HandlerFunc(Handle),
}
server.ListenAndServe()
}
func Handle(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("Expect") == "100-continue" {
w.WriteHeader(http.StatusContinue)
return
}
}
I get response code 200.
If I understand "But I sends a response (200) when return is reached" correctly, you're receiving a
200code instead of a100at the client side.I don't know what
c.Writeris in your example, but you're probably invokingResponseWriter#Writeat some point. See the documentation:From my testing, the response code will also set to be
200if you callResponseWriter#Writeafter invokingResponseWriter.WriteHeader(StatusContinue).So long story short: don't invoke
ResponseWriter#Write. Sincec.Writerseems to be some wrapper around theResponseWriter(since that one doesn't have aWriteHeaderNowmethod), make also sure your wrapping library doesn't write anything to the response body either.