{
"StudentName": "Jay Singh",
"StudentId" : "192865782",
"StudentGroup": "Computer Science"
}
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
type StudentDetails struct {
StudentName string `json:"StudentName"`
StudentId string `json:"StudentId"`
StudentGroup string `json:"StudentGroup"`
}
func main() {
studentDetails := StudentDetails{
StudentName: "Jay Singh",
StudentId: "192865782",
StudentGroup: "Computer Science",
}
body, _ := json.Marshal(studentDetails)
resp, err := http.Post("<https://reqres.in/api/users>", "application/json", bytes.NewBuffer(body))
if err != nil {
panic(err)
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusCreated {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
jsonStr := string(body)
fmt.Println("Response: ", jsonStr)
} else {
fmt.Println("Get failed with error: ", resp.Status)
}
}