Overview
Write method of the ResponseWriter interface in net/http package can be used to send the image or file body in an HTTP response body. When we send a file or image as part of the HTTP response, then the Content-Type response header is
application/octet-stream
In GO a response is represented by the ResponseWriter Interface. Here is the link to the interface –
https://golang.org/pkg/net/http/#ResponseWriter
ResponseWriter interface is used by an HTTP handler to construct an HTTP response. It provides three functions to set the response parameters
- Header – For writing response header
- Write([]byte) – For writing response body
- WriteHeader(statusCode int) – For writing the http status code
Write function can be used to set the response body. It takes a slice of bytes as input. Hence we need to read the file or image into a slice of bytes before passing it as an argument to the Write function. For this we will use the ReadFile function provided by the ioutil package of golang. Also, there is a Header function. This function can be used to set the content type of the response body using the Content-Type header.
w.Header().Set("Content-Type", "application/octet-stream")
Also, note that WriteHeader function can be used to set the HTTP status code for the response.
Example
Let’s see an example of sending a file or an image as part of an HTTP response. Below is the program for the same
package main
import (
"io/ioutil"
"net/http"
)
func main() {
handler := http.HandlerFunc(handleRequest)
http.Handle("/photo", handler)
http.ListenAndServe(":8080", nil)
}
func handleRequest(w http.ResponseWriter, r *http.Request) {
fileBytes, err := ioutil.ReadFile("test.png")
if err != nil {
panic(err)
}
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/octet-stream")
w.Write(fileBytes)
return
}
This is how we read the file or image into a variable in Go
fileBytes, err := ioutil.ReadFile("test.png")
We then pass this variable to the Write function
w.Write(fileBytes)
Note that in the above program we are sending test.png which is present locally at my machine. You can replace it with any other file present on your machine. It could be png or any other file.
Also, we are using the WriteHeader function to specify the 200 http status code. We are also setting the correct header
w.Header().Set("Content-Type", "application/octet-stream")
Run the above program. It will start a server on 8080 port on your local machine. Now make the below curl call to the server. Notice in the curl call that we are saving the output to a local file as well
curl -v -X POST http://localhost:8080/example > temp.png
Output will be something like below
* Connected to localhost (::1) port 8080 (#0)
> POST /example HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.54.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Date: Sat, 10 Jul 2021 19:32:47 GMT
< Content-Type: image/png
< Transfer-Encoding: chunked
<
{ [5846 bytes data]
100 5833 0 5833 0 0 957k 0 --:--:-- --:--:-- --:--:-- 1139k
Now check at the current directory in the terminal from which you made the curl call. A file named test.png will be present which is the same file that was sent by the server.
Also, check out our Golang advance tutorial Series - Golang Advance Tutorial