barthr
barthr

Reputation: 440

Submitting form with golang http library

Oke, I'm currently trying to login in to my school website, with my own Crawler. Altough they have some protection against login. First I do a Get request to the Website so I get the token from the hidden Input field. That token I use in my next Post request to login to the url! But for some reason the http response is that I cannot resubmit the form. But with doing the same in Postman rest client (chrome plugin) I can login!

When I try to submit a form to this url:

postLoginUrl = "?username=%s&password=%s&submit=inloggen&_eventId=submit&credentialsType=ldap&lt=%s"
loginUrl     = "https://login.hro.nl/v1/login"

where %s are filled in credentials

req, err := client.Post(loginUrl, "application/x-www-form-urlencoded", strings.NewReader(uri))

I'm getting as response that the Form cannot be resubmitted.

But when I try it with Postman rest client, I'm allowed to login.

code for Csrf token:

func getCSRFtoken() (key string) {
    doc, err := goquery.NewDocument(loginUrl)
    if err != nil {
        log.Fatal(err)
    }
    types := doc.Find("input")
    for node := range types.Nodes {
        singlething := types.Eq(node)

        hidden_input, _ := singlething.Attr("type")

        if hidden_input == "hidden" {
            key, _ := singlething.Attr("value")
            return key
        }
    }
    return ""
}

goquery.NewDocument is a http.Get()

My question now is, how does the URL get's formatted from the library

Upvotes: 2

Views: 3993

Answers (1)

matt.s
matt.s

Reputation: 1746

Maybe you would be better off using: (c *Client)PostForm(url string, data url.Values) (resp *Response, err error)

from net/http like http://play.golang.org/p/8D6XI6arkz

With the params in url.Values (instead of concatenating the strings, like you are doing now.)

Upvotes: 1

Related Questions