Skip to content

fix(client): set Content-Type: application/json on session at init#443

Merged
mekarpeles merged 1 commit into
masterfrom
fix/session-content-type-header
Jul 1, 2026
Merged

fix(client): set Content-Type: application/json on session at init#443
mekarpeles merged 1 commit into
masterfrom
fix/session-content-type-header

Conversation

@mekarpeles

Copy link
Copy Markdown
Member

Summary

Sets Content-Type: application/json on the requests session at init time.

Without this, ModSecurity WAF rule 920340 blocks all POST /api/save_many requests with 403 — the rule requires Content-Type to be present when Content-Length is non-zero. The requests library does not auto-set Content-Type for string data= 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

from olclient.openlibrary import OpenLibrary
from olclient.config import Config, Credentials

cfg = Config().get_config()
s3 = cfg['s3']
ol = OpenLibrary(credentials=Credentials(access=s3[0], secret=s3[1]))

class Doc:
    def __init__(self, data): self._data = data
    def json(self): return self._data

r = ol.save_many([Doc({'key': '/works/OL45804W', 'type': {'key': '/type/work'}})], 'test')
print(r.status_code)  # 200, not 403

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.
Copilot AI review requested due to automatic review settings July 1, 2026 21:56
@mekarpeles
mekarpeles merged commit 1ba737a into master Jul 1, 2026
4 checks passed

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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/json header on the requests.Session during OpenLibrary initialization.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread olclient/openlibrary.py

def __init__(self, credentials=None, base_url='https://openlibrary.org'):
self.session = requests.Session()
self.session.headers.update({'Content-Type': 'application/json'})
Comment thread olclient/openlibrary.py

def __init__(self, credentials=None, base_url='https://openlibrary.org'):
self.session = requests.Session()
self.session.headers.update({'Content-Type': 'application/json'})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants