You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Fix typo in the client header that could potentially return HTTP error 415 (unsupported media type). The correct spelling is "Content-Type". See Content-Type header.
@javlor, this is actually slightly more complicated than I thought. As summarized by review agent:
Background
This fix was reported as necessary to resolve HTTP 415 (Unsupported Media Type) errors in Google Colab. The root cause is real, but the fix is incorrect and introduces a different contract violation that will behave unpredictably across environments.
Why Google Colab returned 415
Google Colab routes outbound traffic through a Google-managed network proxy. With the original "ContentType": "application/json" (no hyphen), the outgoing POST included two relevant headers:
ContentType: application/json — a malformed, unrecognised header
Content-Type: multipart/form-data; boundary=... — correctly auto-set by requests
The Colab proxy, on encountering the malformed ContentType header alongside a valid Content-Type, appears to have dropped or rewritten the Content-Type before forwarding the request. The server received no valid Content-Type → 415.
Why the proposed fix works in Colab but is wrong
requests sets Content-Type in prepare_body only if it is not already present in the headers (checked via CaseInsensitiveDict):
# from requests/models.pyifcontent_typeand ("content-type"notinself.headers):
self.headers["Content-Type"] =content_type
By supplying "Content-Type": "application/json" in the shared headers dict, the caller's value wins and requests never overwrites it — even when building a multipart/form-data body with files attached. The server now receives:
Content-Type: application/json — declared by our code
Body: multipart-encoded binary data with a boundary
This is a broken HTTP request. It works in Colab only because the SolarFarmer API server is lenient and parses the multipart body regardless of the declared Content-Type. Any stricter server, reverse proxy, or future API version will reject it.
Correct fix
Remove Content-Type from the shared headers dict entirely. requests handles it correctly when left alone:
GET requests: no body, no Content-Type needed
POST with files: auto-set to multipart/form-data; boundary=<boundary>
POST without files: auto-set to application/x-www-form-urlencoded
# solarfarmer/api.py — _make_request()headers= {
"Authorization": f"Bearer {key}",
# Content-Type intentionally omitted — requests sets this automatically# based on whether files are present (multipart) or not (form-encoded).# Forcing it here overrides the boundary parameter and breaks multipart uploads."User-Agent": "solarfarmer-api-sdk/"+__version__,
}
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
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.
Fix typo in the client header that could potentially return HTTP error 415 (unsupported media type). The correct spelling is "Content-Type". See Content-Type header.