- 获取所有environments
package main
import (
"fmt"
"net/http"
"io/ioutil"
)
func main() {
url := "https://api.getpostman.com/environments"
method := "GET"
client := &http.Client {
}
req, err := http.NewRequest(method, url, nil)
if err != nil {
fmt.Println(err)
return
}
req.Header.Add("X-API-Key", "")
res, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(body))
}
- 获取单个environments
package main
import (
"fmt"
"net/http"
"io/ioutil"
)
func main() {
url := "https://api.getpostman.com/environments/%7B%7Benvironment_uid%7D%7D"
method := "GET"
client := &http.Client {
}
req, err := http.NewRequest(method, url, nil)
if err != nil {
fmt.Println(err)
return
}
req.Header.Add("X-API-Key", "")
res, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(body))
}
- 创建environments
package main
import (
"fmt"
"strings"
"net/http"
"io/ioutil"
)
func main() {
url := "https://api.getpostman.com/environments"
method := "POST"
payload := strings.NewReader(`{
"environment": {
"name": "Sample Environment Name (required)",
"values": [
{
"key": "variable_name_1",
"value": "The value of the variable"
},
{
"key": "variable_name_2",
"value": ""
}
]
}
}`)
client := &http.Client {
}
req, err := http.NewRequest(method, url, payload)
if err != nil {
fmt.Println(err)
return
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("X-API-Key", "")
res, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(body))
}
- 更新environments
package main
import (
"fmt"
"strings"
"net/http"
"io/ioutil"
)
func main() {
url := "https://api.getpostman.com/environments/%7B%7Benvironment_uid%7D%7D"
method := "PUT"
payload := strings.NewReader(`{
"environment": {
"name": "New Name",
"values": [
{
"key": "name",
"value": "Bruce Wayne"
},
{
"key": "occupation",
"value": "Vigilante"
}
]
}
}`)
client := &http.Client {
}
req, err := http.NewRequest(method, url, payload)
if err != nil {
fmt.Println(err)
return
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("X-API-Key", "")
res, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(body))
}
- 删除environments
package main标签:return,postman,err,nil,fmt,req,golang,api,Println From: https://blog.51cto.com/wms3001/5786168
import (
"fmt"
"net/http"
"io/ioutil"
)
func main() {
url := "https://api.getpostman.com/environments/%7B%7Benvironment_uid%7D%7D"
method := "DELETE"
client := &http.Client {
}
req, err := http.NewRequest(method, url, nil)
if err != nil {
fmt.Println(err)
return
}
req.Header.Add("X-API-Key", "")
res, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(body))
}