GET Content-Type

Report a typo

Suppose you have a web server at http://127.0.0.1:8080/, which has various endpoints:

  • http://127.0.0.1:8080/hello-world
  • http://127.0.0.1:8080/json
  • http://127.0.0.1:8080/wiki

Your task is to create a function getContentType() that takes an url read from the input as an argument, makes a GET request to the url and returns a string with the Content-Type of the response.

To do this, you'll need to access the Header field within the response struct, use the Get method, and pass to it "Content-Type" as an argument.

For example, getContentType("https://127.0.0.1:8080/hello-world") should return the following string: "text/plain; charset=utf-8".

Sample Input 1:

http://127.0.0.1:8080/hello-world

Sample Output 1:

text/plain; charset=utf-8
Write a program in Go
package main

import (
"fmt"
"log"
"net/http"
)

// Write the code to finish the implementation of `getContentType()` below:
func getContentType(? string) string {
response, err := ?.?(url)
if err != nil {
log.Fatal(err)
}
defer response.Body.Close()

// Access the `Header` field of `response` and pass "Content-Type" to `Get` below:
return ?.?.Get("?")
}

// DO NOT modify the contents of the main() or must() functions!
func main() {
http.HandleFunc("/hello-world", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Hello, World!")
})

http.HandleFunc("/json", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
fmt.Fprintln(w, `{"message": "Hello, JSON!"}`)
})

http.HandleFunc("/wiki", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "<h1>Welcome to the JetBrains Academy Wiki!</h1>")
})

go func() {
___

Create a free account to access the full topic