How to post a request to get cookie values and post new request by the previously obtained cookie By using Go language?

huangapple go评论109阅读模式
英文:

How to post a request to get cookie values and post new request by the previously obtained cookie By using Go language?

问题

如何使用Go语言发布请求以获取cookie值并通过先前获取的cookie发布新请求

这里的第一个POST请求生成一个名为cookie的变量,形式为[SID=pcmPXXx+fidX1xxxX1cuK; Path=/; HttpOnly; SameSite=Strict]",但是我无法将此cookie发送到另一个POST请求(出现错误)。

以下是带有注释的示例Go文件:

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "strings"
  6. "net/http"
  7. "io/ioutil"
  8. "net/http/cookiejar"
  9. )
  10. var client http.Client
  11. func init() {
  12. jar, err := cookiejar.New(nil)
  13. if err != nil {
  14. log.Fatalf("创建cookie jar时出错:%s", err.Error())
  15. }
  16. client = http.Client{
  17. Jar: jar,
  18. }
  19. }
  20. func main() {
  21. urlx := "http://localhost:8080/api/v2/auth/login"
  22. methodx := "POST"
  23. payloadx := strings.NewReader(`username=admin&password=strongadmin`)
  24. client := &http.Client {
  25. }
  26. reqx, err := http.NewRequest(methodx, urlx, payloadx)
  27. if err != nil {
  28. fmt.Println(err)
  29. return
  30. }
  31. reqx.Header.Add("Referer", "http://localhost:8080/")
  32. reqx.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36")
  33. reqx.Header.Add("Content-type", "application/x-www-form-urlencoded; charset=UTF-8")
  34. resx, err := client.Do(reqx)
  35. if err != nil {
  36. fmt.Println(err)
  37. return
  38. }
  39. defer resx.Body.Close()
  40. cookie := resx.Cookies()
  41. fmt.Println(cookie)
  42. bodyx, err := ioutil.ReadAll(resx.Body)
  43. if err != nil {
  44. fmt.Println(err)
  45. return
  46. }
  47. fmt.Println(string(bodyx))
  48. //second request start here
  49. // this var cookies is a dummy cookie that i manually written,
  50. // i need to replace the below variable 'cookies' to previously obtained variable 'cookie' without error
  51. cookies := &http.Cookie{
  52. Name: "SID",
  53. Value: "PW16gD0Ja0OqixXeqpQle1WC/OK19tj+",
  54. MaxAge: 300,
  55. }
  56. url := "http://localhost:8080/api/changes"
  57. method := "POST"
  58. payload := strings.NewReader(`json=%7B%data=valuex%22%3A%22300%22%7D`)
  59. clientx := &http.Client {
  60. }
  61. req, err := http.NewRequest(method, url, payload)
  62. if err != nil {
  63. fmt.Println(err)
  64. return
  65. }
  66. // req.Header.Add("Accept", "text/javascript, text/html, application/xml, text/xml, */*")
  67. req.Header.Add("Referer", "http://localhost:8080/")
  68. req.Header.Add("X-Requested-With", "XMLHttpRequest")
  69. req.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36")
  70. req.Header.Add("Content-type", "application/x-www-form-urlencoded; charset=UTF-8")
  71. req.AddCookie(cookies)
  72. res, err := clientx.Do(req)
  73. if err != nil {
  74. fmt.Println(err)
  75. return
  76. }
  77. defer res.Body.Close()
  78. body, err := ioutil.ReadAll(res.Body)
  79. if err != nil {
  80. fmt.Println(err)
  81. //second request end here
  82. }

请注意,这只是一个示例代码,你需要根据你的实际需求进行适当的修改。

英文:

How to post a request to get cookie values and post new request by the previously obtained cookie By using Go language

here first post request is generating a variable cookie in the form [SID=pcmPXXx+fidX1xxxX1cuK; Path=/; HttpOnly; SameSite=Strict]"

but i can't send this cookie to another post request (getting error).

sample go file with comments

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "strings"
  6. "net/http"
  7. "io/ioutil"
  8. "net/http/cookiejar"
  9. )
  10. var client http.Client
  11. func init() {
  12. jar, err := cookiejar.New(nil)
  13. if err != nil {
  14. log.Fatalf("Got error while creating cookie jar %s", err.Error())
  15. }
  16. client = http.Client{
  17. Jar: jar,
  18. }
  19. }
  20. func main() {
  21. urlx := "http://localhost:8080/api/v2/auth/login"
  22. methodx := "POST"
  23. payloadx := strings.NewReader(`username=admin&password=strongadmin`)
  24. client := &http.Client {
  25. }
  26. reqx, err := http.NewRequest(methodx, urlx, payloadx)
  27. if err != nil {
  28. fmt.Println(err)
  29. return
  30. }
  31. reqx.Header.Add("Referer", "http://localhost:8080/")
  32. reqx.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36")
  33. reqx.Header.Add("Content-type", "application/x-www-form-urlencoded; charset=UTF-8")
  34. resx, err := client.Do(reqx)
  35. if err != nil {
  36. fmt.Println(err)
  37. return
  38. }
  39. defer resx.Body.Close()
  40. cookie := resx.Cookies()
  41. fmt.Println(cookie)
  42. bodyx, err := ioutil.ReadAll(resx.Body)
  43. if err != nil {
  44. fmt.Println(err)
  45. return
  46. }
  47. fmt.Println(string(bodyx))
  48. //second request start here
  49. // this var cookies is a dummy cookie that i manually written,
  50. // i need to replace the below variable 'cookies' to previously obtained variable 'cookie' without error
  51. cookies := &http.Cookie{
  52. Name: "SID",
  53. Value: "PW16gD0Ja0OqixXeqpQle1WC/OK19tj+",
  54. MaxAge: 300,
  55. }
  56. url := "http://localhost:8080/api/changes"
  57. method := "POST"
  58. payload := strings.NewReader(`json=%7B%data=valuex%22%3A%22300%22%7D`)
  59. clientx := &http.Client {
  60. }
  61. req, err := http.NewRequest(method, url, payload)
  62. if err != nil {
  63. fmt.Println(err)
  64. return
  65. }
  66. // req.Header.Add("Accept", "text/javascript, text/html, application/xml, text/xml, */*")
  67. req.Header.Add("Referer", "http://localhost:8080/")
  68. req.Header.Add("X-Requested-With", "XMLHttpRequest")
  69. req.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36")
  70. req.Header.Add("Content-type", "application/x-www-form-urlencoded; charset=UTF-8")
  71. req.AddCookie(cookies)
  72. res, err := clientx.Do(req)
  73. if err != nil {
  74. fmt.Println(err)
  75. return
  76. }
  77. defer res.Body.Close()
  78. body, err := ioutil.ReadAll(res.Body)
  79. if err != nil {
  80. fmt.Println(err)
  81. //second request end here
  82. }

答案1

得分: 1

在第一个请求中,您可以通过以下方式获取cookie:

  1. cookie := resx.Cookies()

这是一个Cookie结构体的切片[]*http.Cookie

然后,如果您想在下一个请求中使用它们,请对它们进行循环并添加:

  1. for _, c := range cookie {
  2. req.AddCookie(c)
  3. }

可以参考这个示例

英文:

In first request You're getting cookies as:

  1. cookie := resx.Cookies()
  • which is slice of Cookie struct []*http.Cookie

then if You want to use them in next request do loop over them and add:

  1. for _, c := range cookie {
  2. req.AddCookie(c)
  3. }

check this example

huangapple
  • 本文由 发表于 2022年3月17日 01:45:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/71501991.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定