cavaliergopher / grab

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

Prevent download if status code is not correct #24

Closed Spedge closed 6 years ago

Spedge commented 6 years ago

Loving the library but been forced to use something else recently, due to the fact that if a 404 Status Code has a body, grab will still download and save it.

In this case, it wasn't the file I wanted but a JSON payload telling me it didn't exist.

If I could whitelist the status codes I wanted to accept, that'd be great (maybe I've missed how to do this)

Thanks!

cavaliercoder commented 6 years ago

Thanks for the suggestion. To keep things simple, I've added a 2XX check in 15e1314, rather than setting a whitelist. The problem with a whitelist is I should probably set a default list. I could use the HTTP spec to build the list, but many servers stray from the spec and could cause undesirable client behaviour.

I'll also look at adding a callback hook, so you can provide custom validation behaviours before the download starts.

cavaliercoder commented 6 years ago

If you'd like more granular validation of response codes, please use Request.BeforeCopy hook: https://godoc.org/github.com/cavaliercoder/grab#Hook. Introduced in 7f4e1fc.