将Curl POST请求转换为Golang代码
将Curl POST请求转换为Golang代码 朋友们,
我目前正在 shell 脚本中使用 curl 命令执行这个 API POST 请求,但我想创建一个 Go 程序来实现这个功能,以替代 shell 脚本中的 curl 命令。同时希望能够从标准输入获取管理 IP、用户名、密码和 FirstAddress 等参数。
以下是相关命令:
curl -sku admin:admin -H "Content-Type: application/json" -X POST https://$FIRST_MGMT_IP/mgmt/tm/ltm/pool -d '{"name": "FirstIPFIXPool", "monitor": "gateway_icmp ", "members": [{"name":"'${FirstAddress}':4739", "address":"'$FirstAddress'"}]}' | python -m json.tool
最佳的实现方式是什么?
更多关于将Curl POST请求转换为Golang代码的实战教程也可以访问 https://www.itying.com/category-94-b0.html
我的项目在某种程度上模拟了curl:https://github.com/mrichman/hargo
也许你可以从中获得灵感。
更多关于将Curl POST请求转换为Golang代码的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
要获取基本结构,您还可以使用 Matt Holt 提供的这个实用工具将 curl 命令转换为 Go 代码:
https://mholt.github.io/curl-to-go/
也许这会对您有所帮助。
实际上,如果提供了 -d 'body' 参数,curl 会默认使用 POST 方法。
但你说得对,确实需要使用 http.Post 而不是 http.PostForm。
你忘记导入 net/url 包了,这样才能使用 url.Values。
然后你的用法有误。url.Values 实际上是 map[string][]string 类型。
此外,最后但同样重要的是,你在初始帖子中展示的 cURL 调用并不是在进行表单提交,而是在请求体中发送 JSON 文档。
http.Post 可能更符合你的需求。
感谢,我试过了,但在这里出现了语法错误:
package main
import (
"fmt"
"os"
"net/http"
)
func main() {
resp, err := http.PostForm("https://10.192.74.73/mgmt/tm/ltm/pool",
url.Values{"name":"FirstIPFIXPool", "monitor": "gateway_icmp ", "members":[{"name":"15.1.1.1:4739", "address":"15.1.1.1"}]})
}
以下是使用 Go 语言实现该 curl POST 请求的代码示例。该代码从标准输入读取参数,并发送等效的 HTTP POST 请求。
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"strings"
)
func main() {
// 从标准输入读取参数
var mgmtIP, username, password, firstAddress string
fmt.Print("Enter Management IP: ")
fmt.Scanln(&mgmtIP)
fmt.Print("Enter Username: ")
fmt.Scanln(&username)
fmt.Print("Enter Password: ")
fmt.Scanln(&password)
fmt.Print("Enter FirstAddress: ")
fmt.Scanln(&firstAddress)
// 构建请求 URL
url := fmt.Sprintf("https://%s/mgmt/tm/ltm/pool", mgmtIP)
// 构建请求体数据
requestBody := map[string]interface{}{
"name": "FirstIPFIXPool",
"monitor": "gateway_icmp",
"members": []map[string]string{
{
"name": fmt.Sprintf("%s:4739", firstAddress),
"address": firstAddress,
},
},
}
jsonData, err := json.Marshal(requestBody)
if err != nil {
fmt.Printf("Error marshaling JSON: %v\n", err)
return
}
// 创建 HTTP 请求
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
if err != nil {
fmt.Printf("Error creating request: %v\n", err)
return
}
// 设置请求头
req.Header.Set("Content-Type", "application/json")
req.SetBasicAuth(username, password)
// 发送请求
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Printf("Error sending request: %v\n", err)
return
}
defer resp.Body.Close()
// 读取响应体
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Printf("Error reading response: %v\n", err)
return
}
// 美化 JSON 输出
var prettyJSON bytes.Buffer
err = json.Indent(&prettyJSON, body, "", " ")
if err != nil {
fmt.Printf("Error formatting JSON: %v\n", err)
return
}
fmt.Println("Response:")
fmt.Println(prettyJSON.String())
}
该代码执行以下操作:
- 从标准输入读取管理 IP、用户名、密码和 FirstAddress 参数。
- 构建与原始 curl 命令相同的 JSON 请求体。
- 设置基本认证头和 Content-Type 头。
- 发送 POST 请求到指定 URL。
- 读取响应并使用
json.Indent进行美化输出,类似于python -m json.tool的功能。
运行该程序时,按照提示输入参数即可发送等效的 POST 请求。

