github-actions commited on
Commit
98bc41c
·
1 Parent(s): ed31618

Auto deploy from GitHub Actions: 64a20ef08bf8b0ac4b4b94f56d66b74190626718

Browse files
Files changed (1) hide show
  1. app.py +25 -3
app.py CHANGED
@@ -115,7 +115,6 @@ def make_api_call(
115
  request_kwargs: Dict[str, Any] = {
116
  "headers": headers,
117
  "timeout": 10,
118
- # Preserve HTTP method when handling redirects ourselves.
119
  "allow_redirects": False,
120
  }
121
  if normalized_method != "GET":
@@ -123,6 +122,29 @@ def make_api_call(
123
 
124
  response = requests.request(normalized_method, url, **request_kwargs)
125
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
126
  if response.status_code in [200, 201]:
127
  return True, response.json()
128
  return False, extract_error_message(response)
@@ -132,8 +154,8 @@ def make_api_call(
132
  return False, "Backend service is not responding. Please try again later."
133
  except requests.exceptions.RequestException:
134
  return False, "Failed to reach backend. Please check your network and API settings."
135
- except Exception as e:
136
- # Log the error but return a generic message to avoid leaking stack traces
137
  return False, "An error occurred while processing your request."
138
 
139
 
 
115
  request_kwargs: Dict[str, Any] = {
116
  "headers": headers,
117
  "timeout": 10,
 
118
  "allow_redirects": False,
119
  }
120
  if normalized_method != "GET":
 
122
 
123
  response = requests.request(normalized_method, url, **request_kwargs)
124
 
125
+ # Some deployments redirect (e.g., HTTP -> HTTPS or canonical host).
126
+ # Follow one redirect while preserving method semantics.
127
+ if response.status_code in {301, 302, 303, 307, 308}:
128
+ redirect_target = response.headers.get("Location")
129
+ if not redirect_target:
130
+ return False, "Backend redirected without a valid target."
131
+
132
+ redirect_url = urljoin(url, redirect_target)
133
+ redirect_method = normalized_method
134
+ redirect_kwargs: Dict[str, Any] = {
135
+ "headers": headers,
136
+ "timeout": 10,
137
+ "allow_redirects": False,
138
+ }
139
+
140
+ # Browsers and many clients switch to GET for 301/302/303 after POST.
141
+ if response.status_code in {301, 302, 303} and normalized_method == "POST":
142
+ redirect_method = "GET"
143
+ elif normalized_method != "GET":
144
+ redirect_kwargs["json"] = data
145
+
146
+ response = requests.request(redirect_method, redirect_url, **redirect_kwargs)
147
+
148
  if response.status_code in [200, 201]:
149
  return True, response.json()
150
  return False, extract_error_message(response)
 
154
  return False, "Backend service is not responding. Please try again later."
155
  except requests.exceptions.RequestException:
156
  return False, "Failed to reach backend. Please check your network and API settings."
157
+ except Exception:
158
+ # Return a generic message to avoid leaking stack traces.
159
  return False, "An error occurred while processing your request."
160
 
161