Colly: Example using JSON POST?

Created on 25 Jun 2018  Â·  4Comments  Â·  Source: gocolly/colly

I am trying to scrape a page from a website that requires authentication first. The site processes logins using POST data and JSON payload. It appears that I need to use func (*Collector) Request and set the accept header first and then the request payload in JSON.

Are there any examples of how to craft this request? I've been stabbing at this for a few hours with no luck.

Most helpful comment

Thanks for the help. For the benefit of anyone else in the same situation, I got it working with this:

payload := []byte(`{"user":{"email":"[email protected]","password":"mypassword"}}`)
c := colly.NewCollector()
c.OnRequest(func(r *colly.Request) {
    r.Headers.Set("Content-Type", "application/json;charset=UTF-8")
})
err := c.PostRaw(URL, payload)

All 4 comments

You can use collector.PostRaw (https://godoc.org/github.com/gocolly/colly#Collector.PostRaw) to send JSON. If you have to set additional headers, you can do it in a collector.OnRequest callback.

Thanks for the help. For the benefit of anyone else in the same situation, I got it working with this:

payload := []byte(`{"user":{"email":"[email protected]","password":"mypassword"}}`)
c := colly.NewCollector()
c.OnRequest(func(r *colly.Request) {
    r.Headers.Set("Content-Type", "application/json;charset=UTF-8")
})
err := c.PostRaw(URL, payload)

but i can not get response.request.body data

but i can not get response.request.body data

i get this problem too,have you solved this promblem?

Was this page helpful?
0 / 5 - 0 ratings