@@ -72,37 +72,48 @@ curl_with_retry() {
72
72
# Add URL
73
73
curl_args+=(" $url " )
74
74
75
- # Execute curl
76
- local response
77
- response=$( curl " ${curl_args[@]} " 2>&1 )
75
+ # Create temporary files for response body and stderr
76
+ local response_file
77
+ response_file=$( mktemp) || { log_error " Failed to create temporary file" ; return 1; }
78
+ local stderr_file
79
+ stderr_file=$( mktemp) || { log_error " Failed to create temporary file" ; rm -f " $response_file " ; return 1; }
80
+
81
+ # Add options to capture status code separately
82
+ curl_args+=(" -w" " %{http_code}" " -o" " $response_file " )
83
+
84
+ # Execute curl and capture status code and stderr
85
+ local status_code
86
+ status_code=$( curl " ${curl_args[@]} " 2> " $stderr_file " )
78
87
local curl_exit_code=$?
79
88
80
89
# Check curl exit code
81
90
if [[ $curl_exit_code -eq 0 ]]; then
82
- local status_code
83
- if [[ -n " $response " ]]; then
84
- status_code=$( echo " $response " | tail -n1)
85
- local response_body=$( echo " $response " | sed ' $d' )
86
-
87
- # Clean up temp file (only if we created it)
88
- if [[ -n " $temp_file " && -z " $data_file " ]]; then
89
- rm -f " $temp_file "
90
- fi
91
-
92
- if [[ " $status_code " =~ ^2[0-9][0-9]$ ]]; then
93
- echo " $response_body "
94
- return 0
95
- else
96
- return 1
97
- fi
91
+ local response_body
92
+ response_body=$( cat " $response_file " 2> /dev/null)
93
+
94
+ # Clean up temporary files
95
+ rm -f " $response_file " " $stderr_file "
96
+
97
+ # Clean up temp data file (only if we created it)
98
+ if [[ -n " $temp_file " && -z " $data_file " ]]; then
99
+ rm -f " $temp_file "
100
+ fi
101
+
102
+ if [[ " $status_code " =~ ^2[0-9][0-9]$ ]]; then
103
+ echo " $response_body "
104
+ return 0
98
105
else
106
+ log_error " HTTP $status_code : Request failed"
99
107
return 1
100
108
fi
101
109
elif [[ $curl_exit_code -eq 28 ]]; then
102
110
# Timeout - retry
111
+ rm -f " $response_file " " $stderr_file "
103
112
retry_count=$(( retry_count + 1 ))
104
113
sleep 1
105
114
else
115
+ # Other curl error - cleanup and exit
116
+ rm -f " $response_file " " $stderr_file "
106
117
break
107
118
fi
108
119
done
0 commit comments