SoFunction
Updated on 2025-03-04

How to send Get and Post requests using Golang

Preface

Recently, I was studying DingTalk robots and found that DingTalk's third-party interface sometimes needs to be accessed with Get or Post requests. Although it can directly simulate sending requests through Apifox, I still want to study how to use Golang to send Get and Post requests.

Get Request

package getUserid
 
import (
	"crypto/tls"
	"encoding/json"
	"io/ioutil"
	"net/http"
	"strings"
	"time"
)
 
type User struct { //We need to pass a parameter mobile when sending a get request, and we encapsulate a corresponding structure	Mobile string `json:"mobile"`
}
type UserResopnseFail struct { //This is to save the failure information in the object created by the structure if the access fails.	Errcode int    `json:"errcode"`
	Errmsg  string `json:"errmsg"`
	Result  struct {
		Userid string `json:"userid"`
	} `json:"result"`
	RequestId string `json:"request_id"`
}
type UserResponseSucc struct {// // This is to save the success information in the object created by the structure if the access is successful	Errcode int    `json:"errcode"`
	Errmsg  string `json:"errmsg"`
	Result  struct {
		Userid string `json:"userid"`
	} `json:"result"`
	RequestId string `json:"request_id"`
}
 
func GetUserID(number,access_token string) (userid string,err error) {
	var client * //Embroidery client	var request * //Encapsulation request	var resp * //Package response	var body []byte
	urlForUserID := "/topapi/v2/user/getbymobile?access_token=" + access_token//Split URL	client = &{Transport: &{ //Make some configurations for the client		TLSClientConfig: &{
			InsecureSkipVerify: true,
		},
	}, Timeout: ( * 5)}
	u := User{
		Mobile: number,
	} //Declare a structure object and put the parameters we pass in	usermarshal, err := (&u) //Serialize the structure into type byte[]	if err != nil {
		return
	}
	
	reqBody := (string(usermarshal))//Another step can be processed here before it can be put into the request below	request, err = (, urlForUserID, reqBody)
	if err != nil {
		return
	}
	resp, err = (request)//Send a request	if err != nil {
		return
	}
	defer ()
	body, err = () //Convert the requested body into byte[]	if err != nil {
		return
	}
	dataSucc := UserResponseSucc{} //Declare a response object	err = (body, &dataSucc)//Deserialize it into a structure object	if err != nil {
		dateFail := UserResopnseFail{}
		err = (body, &dateFail)
		if err != nil {
			return
		}
	}
	return ,err
 
	return
}

The above is Golang sending a simple Get request.

Post request

func Post(Required parameters) (Data to be returned) {
	var client *
	var request *
	var resp *
	var body []byte
	URL := "xxx" 
	client = &{Transport: &{ //Make some configurations for the client		TLSClientConfig: &{
			InsecureSkipVerify: true,
		},
	}, Timeout: ( * 5)}
//This is the request question for post request. Let's first initialize an object	b := Body{
		xxx :xxx,
	}
// Then serialize the structure object	bodymarshal, err := (&b)
	if err != nil {
		return
	}
//Troubleshoot it again	reqBody := (string(bodymarshal))
//Then you can put it in the specific request.	request, err = (, URL, reqBody)
	if err != nil {
		return
	}
	resp, err = (request)
	if err != nil {
		return
	}
	defer ()
	body, err = () //Convert the requested body into byte[]	if err != nil {
		return
	}
//Initialize the structure object	r := Response{}
//Deserialize the requested structure to an object that specifically accepts the return value	err = (body, &r)
	if err != nil {
		return
	}
	// Make a specific logical judgment here and then return	return
}
//This is the request body for sending Post request (we can define it according to the specific interface document)type Body struct {
	xxx   string `json:"xxx"`
}
//This is the return result of the request (we can define it according to the specific interface document)type Response struct {
	Errcode int `json:"errcode"`
	Result Result `json:"result"`
}

Summarize

This is the article about how to use Golang to send Get and Post requests. For more relevant content on Golang to send Get and Post requests, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!