diff --git a/flaresolverr.go b/flaresolverr.go index af9579e..ec6b9fb 100644 --- a/flaresolverr.go +++ b/flaresolverr.go @@ -2,6 +2,7 @@ package flaresolverr import ( "encoding/json" + "fmt" "net/http" "strings" "time" @@ -43,36 +44,45 @@ func (f *Flaresolverr) requestV1(req *V1RequestBase) (*V1ResponseBase, error) { if err != nil { return nil, err } + if resp.StatusCode() != 200 { + return nil, fmt.Errorf("request failed: status code: %v", resp.StatusCode()) + } var res V1ResponseBase err = json.Unmarshal(resp.Body(), &res) if err != nil { return nil, err } - if res.Solution != nil && res.Solution.RawCookies != nil { - for _, cookie := range res.Solution.RawCookies { - sec := int64(cookie.Expires) - nsec := int64((cookie.Expires - float64(sec)) * 1e9) - res.Solution.Cookies = append(res.Solution.Cookies, &http.Cookie{ - Name: cookie.Name, - Value: cookie.Value, - Expires: time.Unix(sec, nsec), - Domain: cookie.Domain, - Path: cookie.Path, - Secure: cookie.Secure, - HttpOnly: cookie.HttpOnly, - }) + if res.Solution != nil { + if res.Solution.RawCookies != nil { + for _, cookie := range res.Solution.RawCookies { + sec := int64(cookie.Expires) + nsec := int64((cookie.Expires - float64(sec)) * 1e9) + res.Solution.Cookies = append(res.Solution.Cookies, &http.Cookie{ + Name: cookie.Name, + Value: cookie.Value, + Expires: time.Unix(sec, nsec), + Domain: cookie.Domain, + Path: cookie.Path, + Secure: cookie.Secure, + HttpOnly: cookie.HttpOnly, + }) + } + } + + if res.Solution.RawResponse != nil { + switch v := res.Solution.RawResponse.(type) { + case string: + res.Solution.Response = res.Solution.RawResponse.(string) + case map[string]any: + jsonBytes, err := json.Marshal(v) + if err != nil { + return nil, err + } + res.Solution.Response = string(jsonBytes) + } } } - switch v := res.Solution.RawResponse.(type) { - case string: - res.Solution.Response = res.Solution.RawResponse.(string) - case map[string]any: - jsonBytes, err := json.Marshal(v) - if err != nil { - return nil, err - } - res.Solution.Response = string(jsonBytes) - } + return &res, nil }