To set the MIME type for an HTTP response in a Go web application, you can make use of the http.ResponseWriter
and http.Request
objects.
Here's an example of how to set the MIME type for a JSON response:
func handler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(`{"message": "Hello, World!"}`))
}
In the above example, w.Header().Set("Content-Type", "application/json")
is used to set the MIME type to JSON. You can replace "application/json"
with the desired MIME type for your response.
Ensure that you call this function before writing any data to the response body using w.Write
or any similar method.
Please note that the MIME type specified should match the actual content being sent.