Skip to content

rest: send Content-Type application/json for JSON bodies - #2783

Open
eisen0419 wants to merge 1 commit into
kubernetes-client:masterfrom
eisen0419:rest-default-json-content-type
Open

eisen0419 wants to merge 1 commit into
kubernetes-client:masterfrom
eisen0419:rest-default-json-content-type

Conversation

@eisen0419

Copy link
Copy Markdown

What type of PR is this?

/kind bug
/kind regression

What this PR does / why we need it:

Summary

Since v37.0.0, generated create_* and delete_* methods whose OpenAPI consumes is ["*/*"] serialize a JSON body without setting Content-Type, causing strict HTTP stacks/intermediaries (such as urllib3-future / niquests) to default str bodies to text/plain and fail with 415 Unsupported Media Type.

When RESTClientObject.request JSON-serializes a non-None body and no Content-Type header is present in any casing, default Content-Type to application/json on a shallow copy of headers, while keeping body-less requests unchanged and keeping scripts/rest_client_patch.diff and scripts/client_go_retry_patch.diff synchronized with scripts/update-client.sh.

  RESTClientObject.request(method, url, headers, body, ...)
    method in ['POST', 'PUT', 'PATCH', 'OPTIONS', 'DELETE']
-     content_type = headers.get('Content-Type')
+     content_type_key, content_type = find_header_case_insensitive(headers, 'content-type')
      if is_json or is_structured_yaml:
        if body is not None:
          request_body = json.dumps(body)
+         if content_type_key is None:
+           headers = dict(headers)
+           headers['Content-Type'] = 'application/json'

Evidence

  • Before:
    BatchV1Api.create_namespaced_job(...) -> POST body JSON, Content-Type: None (415 under urllib3-future)
    BatchV1Api.delete_namespaced_job(..., body=V1DeleteOptions(...)) -> DELETE body JSON, Content-Type: None
    
  • After:
    BatchV1Api.create_namespaced_job(...) -> Content-Type: application/json
    BatchV1Api.delete_namespaced_job(..., body=V1DeleteOptions(...)) -> Content-Type: application/json
    BatchV1Api.delete_namespaced_job(...) [no body] -> Content-Type: None
    pytest kubernetes/utils/rest_test.py kubernetes/utils/keepalive_test.py kubernetes/utils/retry_test.py -> 55 passed
    git apply -R / git apply (update-client.sh 3-patch stack) -> clean, 0 offset, 0 fuzz
    

Merge Danger

Door: two-way

Scoped to RESTClientObject.request and its corresponding regeneration patch stack (scripts/rest_client_patch.diff, scripts/client_go_retry_patch.diff).

Blast Radius: narrow

Only adds Content-Type: application/json when rest.py has already json.dumps-serialized a non-None request body and no Content-Type header (in any casing) was supplied; body-less requests and explicit media types are untouched.

Which issue(s) this PR fixes:

Fixes #2782

Special notes for your reviewer:

scripts/rest_client_patch.diff and the line offset in scripts/client_go_retry_patch.diff were verified via a full reverse/forward git apply round-trip matching scripts/update-client.sh.

Does this PR introduce a user-facing change?

Restore default `Content-Type: application/json` header in the synchronous REST client when sending JSON-serialized request bodies (such as `create_*` and `delete_*` calls with `consumes: ["*/*"]`).

Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.:


Since the v37 generator, create_* and delete_* operations whose OpenAPI
consumes is */* serialize a JSON body and omit Content-Type. v36 set
that header in rest.py. An intermediary that guesses a type for a bare
string body, such as urllib3-future, then sends text/plain and the API
server returns 415.

When RESTClientObject JSON-serializes a body (body is not None) and no
Content-Type is present in any casing, set application/json. Leave
body-less requests unchanged: RFC 9110 section 8.3 only asks for the
header when the message has content. Keep an explicit media type,
including a lowercase header name, and copy the header map before
inserting the default so request() does not mutate the caller's dict.

Generated *_api.py serializers still ignore _content_type for */*
operations; those files are regenerated, and _headers remains the
override. kubernetes.aio already defaults the header.

Refresh scripts/rest_client_patch.diff and move the client-go retry
hunk so update-client.sh applies the stack with no offset.
@kubernetes-prow kubernetes-prow Bot added release-note Denotes a PR that will be considered when it comes time to generate release notes. kind/bug Categorizes issue or PR as related to a bug. labels Nov 14, 2026
@kubernetes-prow

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: eisen0419
Once this PR has been reviewed and has the lgtm label, please assign roycaihw for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@kubernetes-prow kubernetes-prow Bot added the kind/regression Categorizes issue or PR as related to a regression from a prior release. label Nov 14, 2026
@kubernetes-prow
kubernetes-prow Bot requested review from roycaihw and yliaog November 14, 2026 12:46
@kubernetes-prow

Copy link
Copy Markdown
Contributor

Welcome @eisen0419!

It looks like this is your first PR to kubernetes-client/python 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes-client/python has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@kubernetes-prow kubernetes-prow Bot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Nov 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/bug Categorizes issue or PR as related to a bug. kind/regression Categorizes issue or PR as related to a regression from a prior release. release-note Denotes a PR that will be considered when it comes time to generate release notes. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Since v37.0.0, create_* and delete_* calls send a body without a Content-Type header

1 participant