-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve Payload CMS data fetching implementation #29
Conversation
…tion Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍🏽
It was observed that requests with forwarded HTTP headers from the web application were being redirected to a 404 page, which pointed to the charter.africa site. This appears to be an unrelated issue that requires further investigation.
However, discarding (not forwarding) the HTTP request headers from the client (React Application) when making a request to Payload CMS via the Flask backend resolves this issue.
Can we include an example dump of headers that cause redirect/errors?
server/views/cms/documents.py
Outdated
try: | ||
headers = { | ||
'Authorization': f'users API-Key {API_KEY}', | ||
'Content-Type': 'application/json' | ||
} | ||
|
||
if application_name: | ||
headers['Cs-App'] = application_name | ||
|
||
escaped_args = {k: html.escape(v) for k, v in (params or {}).items()} | ||
|
||
response = requests.get(url, params=escaped_args, headers=headers) | ||
response.raise_for_status() | ||
return response.json(), response.status_code | ||
|
||
except requests.RequestException as e: | ||
logger.error(f'Request failed: {str(e)}') | ||
return jsonify({'message': 'An internal error has occurred.'}), 500 | ||
|
||
except ValueError as e: | ||
logger.error(f'Invalid JSON response: {str(e)}') | ||
return jsonify({'message': 'An internal error has occurred.'}), 500 | ||
|
||
except Exception as e: | ||
logger.error(f'Unexpected error: {str(e)}') | ||
return jsonify({'message': 'An internal error has occurred.'}), 500 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
try: | |
headers = { | |
'Authorization': f'users API-Key {API_KEY}', | |
'Content-Type': 'application/json' | |
} | |
if application_name: | |
headers['Cs-App'] = application_name | |
escaped_args = {k: html.escape(v) for k, v in (params or {}).items()} | |
response = requests.get(url, params=escaped_args, headers=headers) | |
response.raise_for_status() | |
return response.json(), response.status_code | |
except requests.RequestException as e: | |
logger.error(f'Request failed: {str(e)}') | |
return jsonify({'message': 'An internal error has occurred.'}), 500 | |
except ValueError as e: | |
logger.error(f'Invalid JSON response: {str(e)}') | |
return jsonify({'message': 'An internal error has occurred.'}), 500 | |
except Exception as e: | |
logger.error(f'Unexpected error: {str(e)}') | |
return jsonify({'message': 'An internal error has occurred.'}), 500 | |
headers = { | |
'Authorization': f'users API-Key {API_KEY}', | |
'Content-Type': 'application/json' | |
} | |
if application_name: | |
headers['Cs-App'] = application_name | |
try: | |
escaped_args = {k: html.escape(v) for k, v in (params or {}).items()} | |
response = requests.get(url, params=escaped_args, headers=headers) | |
return response.json(), response.status_code | |
except requests.RequestException: | |
logger.exception('Request failed') | |
except ValueError: | |
logger.exception('Invalid JSON response') | |
except Exception: | |
logger.exception('Unexpected error') | |
return jsonify({'message': 'An internal error has occurred.'}), 500 |
- In a
try
/except
,logger.exception
includese
by default (unlikelogger.error
). - Do we need
response.raise_for_status()
? - Reduce empty lines
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool, made the changes
Regarding 2. response.raise_for_status()
:
I think we we do need it, the motive was to throw a RequestException
for the 404 response that is coming with HTML content (from charter.africa
)...but I'm also open to let it throw an Invalid JSON response
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we we do need it, the motive was to throw a RequestException for
Cool, cool... we can leave it there for better logs I guess.
Co-authored-by: Clemence Kyara <[email protected]>
Sure here are the dumps, I did more tests on this, it appears that the
|
Thanks @m453h. Based on the definition of ... anyways, we can pick this up tomorrow. |
Yes that's correct 💯, I made a slight change by just removing the header, and let Python requests set it, it does fix the issue |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
This pull request addresses an issue encountered when accessing content from the Payload CMS API deployed in production.
It was observed that requests with forwarded HTTP headers from the web application were being redirected to a 404 page, which pointed to the
charter.africa
site. This appears to be an unrelated issue that requires further investigation.However, discarding (not forwarding) the HTTP request headers from the client (React Application) when making a request to Payload CMS via the Flask backend resolves this issue.
Minor improvements have been implemented by introducing a unified function to handle all Payload CMS API requests and error handling. Furthermore, the existing Flask view functions have been refactored to make use of this function.