Send POST Request using Go

http.Post function

package main

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

func main() {
    data := "name=John&age=20"

    response, err := http.Post(
        "https://httpbin.org/post",
        "application/x-www-form-urlencoded",
        bytes.NewBuffer([]byte(data)),
    )
    if err != nil {
        log.Fatalln(err)
    }
    defer response.Body.Close()

    body, err := io.ReadAll(response.Body)
    if err != nil {
        log.Fatalln(err)
    }
    content := string(body)

    fmt.Println(content)
}

Leave a Comment

Cancel reply

Your email address will not be published.