diff --git a/anticaptcha.go b/anticaptcha.go index 80ac493..4c33d8a 100644 --- a/anticaptcha.go +++ b/anticaptcha.go @@ -3,10 +3,12 @@ package anticaptcha import ( "bytes" "encoding/json" + "github.com/pkg/errors" "log" "net/http" "net/url" "time" + "reflect" ) var ( @@ -47,6 +49,9 @@ func (c *Client) createTaskRecaptcha(websiteURL string, recaptchaKey string) (fl responseBody := make(map[string]interface{}) json.NewDecoder(resp.Body).Decode(&responseBody) // TODO treat api errors and handle them properly + if reflect.ValueOf(responseBody["taskId"]).IsNil() { + return "", errors.Errorf("Failed to get a response") + } return responseBody["taskId"].(float64), nil } @@ -104,6 +109,10 @@ func (c *Client) SendRecaptcha(websiteURL string, recaptchaKey string) (string, break } } + + if response["solution"] == nil { + return "", errors.Errorf("Failed to get a response") + } return response["solution"].(map[string]interface{})["gRecaptchaResponse"].(string), nil } @@ -134,6 +143,9 @@ func (c *Client) createTaskImage(imgString string) (float64, error) { // Decode response responseBody := make(map[string]interface{}) json.NewDecoder(resp.Body).Decode(&responseBody) + if reflect.ValueOf(responseBody["taskId"]).IsNil() { + return "", errors.Errorf("Failed to get a response") + } // TODO treat api errors and handle them properly return responseBody["taskId"].(float64), nil }