Sending data from client to server in a body - REST web service

I am developing a client-server application using RESTful web services.

I want to ask for user input on the client and send it in a body to the server and use that name in the rest of my program.

Below is a part of my program:


 func main() {
       //getting input
        fmt.Println("Please enter your name: ")
        reader := bufio.NewReader(os.Stdin)
        myName, _ := reader.ReadString('\n')
        myName = strings.TrimSpace(myName)

        client := &http.Client{
            CheckRedirect: nil,
        reply, err := http.NewRequest("GET", "http://localhost:8080/", nil)
        reply.Header.Add("username", myName)
       if err != nil {


func CreateClient(w http.ResponseWriter, r *http.Request) {

    clientName := r.Header.Get("username")
    cli := &Client{
        currentRoom:   nil, //starts as nil because the user is not initally in a room
        outputChannel: make(chan string),
        name:          clientName,
    Members = append(Members, cli)

    reply :=

    fmt.Fprintf(w, reply)

Can someone tell me how I can send user input in the body to the server?

By making a HTTP POST request using the net/http package. Use one of these:

Check this example.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.