Checking body

Report a typo

There is a running server where you can create a new user. To do so, you need to use a POST HTTP request to the address http://127.0.0.1:8080/user. To make a valid request to the server, you need to specify a not-empty name in the body of request, and the server will respond you with the id of the newly created user. Please, print the received id, and you'll solve the task!

To make a post request with a body, you can use the example from the theory step:
http.Post(serverAddress, "text/plain", bytes.NewBufferString(name))

Sample Input 1:

100

Sample Output 1:

100
Write a program in Go
package main

import (
"bytes"
"fmt"
"io"
"net/http"
"time"
)

func main() {
// Write your code here
}

// DO NOT MODIFY the contents of the init() or must() functions!
func init() {
const timeout = 5 * time.Second

var input string
fmt.Scan(&input)

go func() {
http.HandleFunc("/user", func(w http.ResponseWriter, r *http.Request) {
all, err := io.ReadAll(r.Body)
must(err)
if r.Method != http.MethodPost || len(all) == 0 {
w.WriteHeader(http.StatusBadRequest)
return
}
if _, err = w.Write([]byte(input)); err != nil {
panic(err)
}
})
must(http.ListenAndServe(":8080", nil))
}()

___

Create a free account to access the full topic