如何发送带有登录数据的 GET 请求并将 cookie 数据保存到 txt 文件?

我想发送带有登录数据的 GET 请求并将 cookie 数据保存到 txt 文件。


我有一个卷曲数据


   curl -c cookies.txt 'http://localhost:8080/api/v2/auth/login' \

  -H 'sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="98"' \

  -H 'sec-ch-ua-mobile: ?1' \

  -H 'User-Agent: Mozilla/5.0 (Linux; Android 9; BND-AL10) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.106 Mobile Safari/537.36' \

  -H 'Content-type: application/x-www-form-urlencoded; charset=UTF-8' \

  -H 'Accept: text/javascript, text/html, application/xml, text/xml, */*' \

  -H 'Referer: http://localhost:8080/' \

  -H 'X-Requested-With: XMLHttpRequest' \

  -H 'sec-ch-ua-platform: "Android"' \

  --data-raw 'username=admin&password=pasxxord7' \

  --compressed

我需要将这个 curl 转换为 Go;我试过在线转换器,它出错了,我认为他们没有任何能力将 cookie 存储到 txt 文件。


我有一个示例 Go 文件,它不能发送用户名和密码,但我认为它确实保存了 cookies.txt 文件:


我的 main.go 内容:


package main


import "net/http/cookiejar"

import "net/http"

import "encoding/json"

import "io/ioutil"

import "fmt"


func main(){

    cookies := make(map[string][]*http.Cookie)

    jar, _ := cookiejar.New(nil)

    client := http.Client{Jar: jar}

    r, err := client.Get("http://localhost:8080/api/v2/auth/login")

    if err != nil {

        fmt.Println(err)

        return

    }

    siteCookies := jar.Cookies(r.Request.URL)

    cookies[r.Request.URL.String()] = siteCookies

    data, _ := json.Marshal(cookies)

    ioutil.WriteFile("cookies.txt", data, 0644)

}

我需要主要将这个curl标志和数据转换并添加--data-raw 'username=admin&password=pasxxord7'到Go中。


当年话下
浏览 150回答 1
1回答

杨魅力

当您使用 curl 标志时--data-raw发送POST请求。您可以使用详细输出标志来验证这一点-v,例如:$ curl -c cookies.txt 'http://requestbin.net/r/40vmgerp' \&nbsp; -H 'sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="98"' \&nbsp; -H 'sec-ch-ua-mobile: ?1' \&nbsp; -H 'User-Agent: Mozilla/5.0 (Linux; Android 9; BND-AL10) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.106 Mobile Safari/537.36' \&nbsp; -H 'Content-type: application/x-www-form-urlencoded; charset=UTF-8' \&nbsp; -H 'Accept: text/javascript, text/html, application/xml, text/xml, */*' \&nbsp; -H 'Referer: http://localhost:8080/' \&nbsp; -H 'X-Requested-With: XMLHttpRequest' \&nbsp; -H 'sec-ch-ua-platform: "Android"' \&nbsp; --data-raw 'username=admin&password=pasxxord7' \&nbsp; --compressed -v* Connected to requestbin.net (172.67.190.62) port 80 (#0)> POST /r/40vmgerp HTTP/1.1 <--------------------> Host: requestbin.net> Accept-Encoding: deflate, gzip> sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="98"> sec-ch-ua-mobile: ?1...通常 GET 请求不用于在正文中传输数据,因此在给定正确POST时使用 as curl 会自动执行。data-raw根据您的示例标题和数据,data-raw您似乎正在使用表单值,也可以将其指定为:curl localhost -F 'username=admin' -F 'password=pasxxord7'这将添加内容类型 multipart/form-data 标头并自动作为 POST 发送。这也具有进行任何必要转义的优点。要在 Go 中重新创建您的 curl 请求,如下所示:package mainimport (&nbsp; &nbsp; "encoding/json"&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "net/http"&nbsp; &nbsp; "net/url"&nbsp; &nbsp; "os"&nbsp; &nbsp; "strings")func main() {&nbsp; &nbsp; data := url.Values{}&nbsp; &nbsp; data.Set("username", "admin")&nbsp; &nbsp; data.Set("password", "pasxxord7")&nbsp; &nbsp; body := strings.NewReader(data.Encode())&nbsp; &nbsp; req, err := http.NewRequest(http.MethodPost, "https://requestbin.net/r/40vmgerp", body)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; panic(err)&nbsp; &nbsp; }&nbsp; &nbsp; // Omitted other headers for brevity.&nbsp; &nbsp; req.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")&nbsp; &nbsp; req.Header.Set("Accept", "text/javascript, text/html, application/xml, text/xml, */*")&nbsp; &nbsp; resp, err := http.DefaultClient.Do(req)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; panic(err)&nbsp; &nbsp; }&nbsp; &nbsp; defer resp.Body.Close()&nbsp; &nbsp; cookies := resp.Cookies()&nbsp; &nbsp; if len(cookies) > 0 {&nbsp; &nbsp; &nbsp; &nbsp; cookieFile, err := os.Create("cookies.txt")&nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; panic(err)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; defer cookieFile.Close()&nbsp; &nbsp; &nbsp; &nbsp; encoder := json.NewEncoder(cookieFile)&nbsp; &nbsp; &nbsp; &nbsp; for _, cookie := range cookies {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // fmt.Fprintln(cookieFile, cookie.String()) // write plain text to file&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if err := encoder.Encode(cookie); err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; panic(err)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("No cookies found.")&nbsp; &nbsp; }}请注意,上面的示例使用 RequestBin 而不是 localhost,但如果您添加其他标头,则应该是完整的。您不需要使用 Cookie Jar,因为它用于在传出请求中存储和发送 cookie。看来您只对存储从服务器发回的 cookie 感兴趣,因此您需要.Cookies()在响应上调用该方法并使用返回的内容。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go