cavaliergopher / grab

A download manager package for Go
BSD 3-Clause "New" or "Revised" License
1.39k stars 150 forks source link

Passing form data on GET requests #89

Closed abelikoff closed 3 years ago

abelikoff commented 3 years ago

I'm trying to download from a website which is a bit peculiar. In order to initiate the download it uses a GET request but also requires form data (as key/value pairs):

HTML Form URL Encoded: application/x-www-form-urlencoded
    Form item: "download" = "Download"
        Key: download
        Value: Download

What would be the recommended way to set the form parameters?

I've tried setting the Form parameter directly via

        params := url.Values{
            "download": {"Download"},
        }

        req.HTTPRequest.Form = params

..but it doesn't seem to pass the form data to the server. Is there a recommended way to accomplish this?

abelikoff commented 3 years ago

OK, my mistake - the expectation is to POST a form with the values above. After some trial and error, the following code solved the problem:

        params := url.Values{
            "download": {"Download"},
        }

        encoded := params.Encode()
        payloadBuf := new(bytes.Buffer)
        payloadBuf.WriteString(encoded)
        req.HTTPRequest, err = http.NewRequest("POST", url, payloadBuf)
        req.HTTPRequest.Header.Set("Referer", url)
        req.HTTPRequest.Header.Set("Content-Type", "application/x-www-form-urlencoded")