0
votes

I'm writing a Go server and client and I can't figure out how write an interim response (status code 1xx). I've searched for documentation and examples for this but haven't found anything. My searching did turn up some information about chunked responses but I don't think that is quite what I need.

I really just want to send a interim response to let the Client know that the request has been recieved (and some information has been validated) while the server gets more data for the final response. The request will be made using ajax on my javascript client. I think I can figure out the client part on my own but I'm new to Go and I could really use some help with writing the response.

I don't have a lot of relevant code to show because I am completely lost as to where to start. But here is the very basic Go server that I am trying to build off of ...if that helps.

package main

import (
    "fmt"
    "net/http"
)

func handle(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "existence is pain")
}

func main() {
    http.HandleFunc("/", handle)
    http.ListenAndServe(":8000", nil)
}

So this is my starting point and where I got stuck. I want to be able to do something like

//validate request
//send interim response
//get more data
//send complete response

Is there a net/http function that allows me to send an interim response? Should I not be using http.HandleFunc() for something like this? (and what should I be using?) If anyone has an example of this or can point me in the right direction, that would help.

Also, sorry if "interim" isn't the accepted jargon. If that caused confusion, here is my source: https://msdn.microsoft.com/en-us/library/cc246721.aspx

2

2 Answers

2
votes

If the request has the Expect: 100-continue header, then the net/http server sends "HTTP/1.1 100 Continue" on first read of the request body.

The logic for the application is:

  • Is the request OK?
    • If yes, then read the request body and proceed as normal.
    • if no, then respond with an error. Do not read the request body.
0
votes
func handle(w http.ResponseWriter, r *http.Request) {
    w.WriteHeader(http. StatusContinue) // 100 code
    fmt.Fprintf(w, "existence is pain")
}