如何在POST呼叫中发送表单数据

时间:2019-12-14 18:30:35

标签: go

我正在尝试从Go调用POST REST的登录调用,该调用应返回带有令牌的Cookies + Cookies的HTML页面。该呼叫与POSTMAN正常工作。但无法以编程方式完成。

Postman Output

我尝试了以下代码:

func login(w http.ResponseWriter, req *http.Request) {

    formData := url.Values{}
    formData.Set("zeroUserName", "admin")
    formData.Set("zeroPassword", "admin4sys")

    http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
    response , err := http.PostForm("https://9.42.121.175/login/" , formData)


    if err != nil {
        fmt.Printf("The HTTP request failed with error %s\n", err)
    } else {
        data, _ := ioutil.ReadAll(response.Body)
        fmt.Println("\response is " , response)
        fmt.Println("\nCookies are " , response.Cookies())
        for _, cookie := range response.Cookies() {
        fmt.Println("\nFound a cookie named:", cookie.Name)
        }
        //fmt.Println(string(data))
        fmt.Fprintf(w, string(data))
    }
}

也尝试过

request, _ := http.NewRequest("POST", url, payload)

但是没有用。

0 个答案:

没有答案