您的位置 首页 golang

golang post,get,put,delete请求示例代码

package main
import (
 "fmt"
 " io /ioutil"
 "net/http"
 "strings"
)
func Post() {
 url := ""
 payload := strings.NewReader("a=111")
 req, _ := http.NewRequest("POST", url, payload)
 req. Header .Add("Date", "Tue, 11 Sep 2018 10:57:09 GMT")
 req.Header.Add("Authorization", "Pan Do ra oqSBNbmgAAGI155F6MJ3N2Tk9ruL_6XQpx-uxkkg:8bUg3Iy5CVzU3vXTTf5v4XF2XjQ=:eyJyZXNvdXJjZSI6Ii92Mi9yZXBvcy93aF9mbG93RGF0YVNvdXJjZTEvZGF0YSIsImV4cGlyZXMiOjE1MzY2OTkwODgsImNvbnRlbnRNRDUiOiIiLCJjb250ZW50VHlwZSI6InRleHQvcGxhaW4iLCJoZWFkZXJzIjoiIiwibWV0aG9kIjoiUE9TVCJ9")
 req.Header.Add("Content-Type", "text/plain")
 res, _ := http.DefaultClient.Do(req)
 defer res.Body. Close ()
 body, _ := ioutil.ReadAll(res.Body)
 fmt.Println(res)
 fmt.Println(string(body))
}
func Get() {
 url := ""
 req, _ := http.NewRequest("GET", url,  nil )
 req.Header.Add("Authorization", "Pandora oqSBNbmgAAGI155F6MJ3N2Tk9ruL_6XQpx-uxkkg:tGCY3xCsgybHd5IjcDakuPEJob8=:eyJyZXNvdXJjZSI6Ii92Mi9yZXBvcy93aF9mbG93RGF0YVNvdXJjZTEiLCJleHBpcmVzIjoxNTM2NzU4NjQ3LCJjb250ZW50TUQ1IjoiIiwiY29udGVudFR5cGUiOiIiLCJoZWFkZXJzIjoiIiwibWV0aG9kIjoiR0VUIn0=")
 res, _ := http.DefaultClient.Do(req)
 defer res.Body.Close()
 body, _ := ioutil.ReadAll(res.Body)
 fmt.Println(res)
 fmt.Println(string(body))
}
func  PUT () {
 url := ""
 payload := strings.NewReader("{\n \"schema\": [\n {\n \"key\": \"a\",\n \"valtype\": \"string\",\n \"required\": false\n }\n ]\n}")
 req, _ := http.NewRequest("PUT", url, payload)
 req.Header.Add("Content-Type", "application/ json ")
 req.Header.Add("Authorization", "Pandora oqSBNbmgAAGI155F6MJ3N2Tk9ruL_6XQpx-uxkkg:yKx_OYDtI3njD7-c7Y87Oov0GpI=:eyJyZXNvdXJjZSI6Ii92Mi9yZXBvcy93aF9mbG93RGF0YVNvdXJjZTEiLCJleHBpcmVzIjoxNTM2NzU1MjkwLCJjb250ZW50TUQ1IjoiIiwiY29udGVudFR5cGUiOiJhcHBsaWNhdGlvbi9qc29uIiwiaGVhZGVycyI6IiIsIm1ldGhvZCI6IlBVVCJ9")
 req.Header.Add("Date", "Wed, 12 Sep 2018 02:10:09 GMT")
 res, _ := http.DefaultClient.Do(req)
 defer res.Body.Close()
 body, _ := ioutil.ReadAll(res.Body)
 fmt.Println(res)
 fmt.Println(string(body))
}
func Delete() {
 url := ""
 req, _ := http.NewRequest("DELETE", url, nil)
 req.Header.Add("Authorization", "Pandora oqSBNbmgAAGI155F6MJ3N2Tk9ruL_6XQpx-uxkkg:o56-nIwtgTzUX80YCNpbcjUL8iM=:eyJyZXNvdXJjZSI6Ii92Mi9yZXBvcy93aF9mbG93RGF0YVNvdXJjZTEiLCJleHBpcmVzIjoxNTM2NzU4ODE2LCJjb250ZW50TUQ1IjoiIiwiY29udGVudFR5cGUiOiIiLCJoZWFkZXJzIjoiIiwibWV0aG9kIjoiREVMRVRFIn0=")
 res, _ := http.DefaultClient.Do(req)
 defer res.Body.Close()
 body, _ := ioutil.ReadAll(res.Body)
 fmt.Println(res)
 fmt.Println(string(body))
}
 

文章来源:智云一二三科技

文章标题:golang post,get,put,delete请求示例代码

文章地址:https://www.zhihuclub.com/86475.shtml

关于作者: 智云科技

热门文章

发表评论

您的电子邮箱地址不会被公开。

网站地图