From d2eb26d4ac33f90f118d07a2ca7428a32bf76236 Mon Sep 17 00:00:00 2001
From: nite07 <admin@nite07.com>
Date: Thu, 27 Feb 2025 08:16:18 +1100
Subject: [PATCH] fix requestV1 requestV1 error

---
 flaresolverr.go | 56 +++++++++++++++++++++++++++++--------------------
 1 file changed, 33 insertions(+), 23 deletions(-)

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
 }