@@ -72,37 +72,48 @@ curl_with_retry() {
7272 # Add URL
7373 curl_args+=(" $url " )
7474
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 " )
7887 local curl_exit_code=$?
7988
8089 # Check curl exit code
8190 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
98105 else
106+ log_error " HTTP $status_code : Request failed"
99107 return 1
100108 fi
101109 elif [[ $curl_exit_code -eq 28 ]]; then
102110 # Timeout - retry
111+ rm -f " $response_file " " $stderr_file "
103112 retry_count=$(( retry_count + 1 ))
104113 sleep 1
105114 else
115+ # Other curl error - cleanup and exit
116+ rm -f " $response_file " " $stderr_file "
106117 break
107118 fi
108119 done
0 commit comments