fix(client): set Content-Type: application/json on session at init#443
Merged
Conversation
Without this, ModSecurity WAF (rule 920340) blocks POST requests to /api/save_many with 403 — Content-Length is non-zero but no Content-Type header is sent because requests does not auto-set it for string data bodies.
There was a problem hiding this comment.
Pull request overview
This PR aims to prevent ModSecurity WAF 403s on JSON write endpoints (notably POST /api/save_many) by ensuring a Content-Type: application/json header is present on requests made by the OpenLibrary client session.
Changes:
- Set a default
Content-Type: application/jsonheader on therequests.SessionduringOpenLibraryinitialization.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
|
||
| def __init__(self, credentials=None, base_url='https://openlibrary.org'): | ||
| self.session = requests.Session() | ||
| self.session.headers.update({'Content-Type': 'application/json'}) |
|
|
||
| def __init__(self, credentials=None, base_url='https://openlibrary.org'): | ||
| self.session = requests.Session() | ||
| self.session.headers.update({'Content-Type': 'application/json'}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Sets
Content-Type: application/jsonon the requests session at init time.Without this, ModSecurity WAF rule 920340 blocks all
POST /api/save_manyrequests with 403 — the rule requiresContent-Typeto be present whenContent-Lengthis non-zero. Therequestslibrary does not auto-setContent-Typefor stringdata=bodies, so the header was never being sent.Setting it at the session level ensures it is inherited by all subsequent requests (login, save_many, etc.).
Test