golang post get put delete 请求实例代码

实例如下:

package main
 
import (
	"fmt"
	"io/ioutil"
	"net/http"
	"strings"
)
 
 
func Post() {
 
	url := "http://xxxxx:8080/v2/repos/wh_flowDataSource1/data"
 
	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", "oqSBNbmgAAGI155F6MJ3N2Tk9ruL_6XQpx-uxkkg:8bUg3Iy5CVzU3vXyJyZXNvdXJjZSI6Ii92Mi9yZXBvcy93aF9mbG93RGF0YVNvdXJjZTEvZGF0YSIsImV4cGlyZXMiOjE1MzY2OTkwODgsIIiLCJjb250ZW50VHlwZSI6InRleHQvcGxhaW4iLCJoZWFkZXJzIjoiIiwibWV0aG9kIjoiUE9TVCJ9")
	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 := "http://xxxxx:8080/v2/repos/wh_flowDataSource1"
 
	req, _ := http.NewRequest("GET", url, nil)
 
	req.Header.Add("Authorization", "BNbmgAAGI155F6MJ3N2Tk9ruL_6XQpx-uxkkg:tGCY3xCsgybHd5IjcDMi9yZXBvcy93aF9mbG93RGF0YVNvdXJjZTEiLCJleHBpcmVzIjoxNTM2NzU4NjQ3LCJjb250ZW5VudFR5cGUiOiIiLCJoZWFkZXJzIjoiIiwibWV0aG9kIjoiR0VUIn0=")
 
	res, _ := http.DefaultClient.Do(req)
 
	defer res.Body.Close()
	body, _ := ioutil.ReadAll(res.Body)
 
	fmt.Println(res)
	fmt.Println(string(body))
 
}
 
 
 
func Put() {
 
	url := "http://xxxxx:8080/v2/repos/wh_flowDataSource1"
 
	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", "bmgAAGI155F6MJ3N2Tk9ruL_6XQpx-uxkkg:yKx_OYDtI3njD7-c7Y87Oov0GpI=:eyJyZXNvdXJBvcy93aF9mbG93RGF0YVNvdXJjZTEiLCJleHBpcmVzIjoxNTM2NzU1MjkwLCJjb250ZW50TUQ1IjoiIiwiY29udGVudFR5cGUiOiJhcHBsaWNhdGlvbi9qc29uIiwiaGVhZGVycyI6IiIsIm1ldGhvZCI6IlBVVCJ9")
	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 := "http://xxxxx:8080/v2/repos/wh_flowDataSource1"
 
	req, _ := http.NewRequest("DELETE", url, nil)
 
	req.Header.Add("Authorization", "5F6MJ3N2Tk9ruL_6XQpx-uxkkg:o56-nIwtgTzUX80YCNpbcjUL8iM=:eyJyZXNvdXJjZSI6IF9mbG93RGF0YVNvdXJjZTEiLCJleHBpcmVzIjoxNTM2NzU4ODE2LCJjb250ZW50TUQ1IjoiIiwiY29udGVudFR5cGUiOiIiLCJoZWFkZXJzIjoiIiwibWV0aG9kIjoiREVMRVRFIn0=")
 
	res, _ := http.DefaultClient.Do(req)
 
	defer res.Body.Close()
	body, _ := ioutil.ReadAll(res.Body)
 
	fmt.Println(res)
	fmt.Println(string(body))
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值