Skip to content

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

Merged
kubernetes-prow[bot] merged 1 commit into
kubernetes-client:masterfrom
eisen0419:rest-default-json-content-type
Nov 21, 2026
Merged

kubernetes-prow[bot] merged 1 commit into
kubernetes-client:masterfrom
eisen0419:rest-default-json-content-type

Conversation

@eisen0419

Copy link
Copy Markdown
Contributor

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. kind/regression Categorizes issue or PR as related to a regression from a prior release. labels 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
@yliaog

yliaog commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

the file is generated by openapi-generator, could you check to fix it upstream there ?

@yliaog

yliaog commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

/close

@kubernetes-prow kubernetes-prow Bot closed this Nov 21, 2026
@kubernetes-prow

Copy link
Copy Markdown
Contributor

@yliaog: Closed this PR.

Details

In response to this:

/close

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@yliaog

yliaog commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

/reopen

@kubernetes-prow kubernetes-prow Bot reopened this Nov 21, 2026
@kubernetes-prow

Copy link
Copy Markdown
Contributor

@yliaog: Reopened this PR.

Details

In response to this:

/reopen

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@eisen0419

Copy link
Copy Markdown
Contributor Author

Yes, kubernetes/client/rest.py is generated from OpenAPITools/openapi-generator's modules/openapi-generator/src/main/resources/python/rest.mustache, where RESTClientObject.request() currently passes headers unchanged after json.dumps(body) when Content-Type is absent (I checked OpenAPITools/openapi-generator and did not find an open issue or PR for this yet).

Because scripts/update-client.sh already carries a local patch stack on rest.py (scripts/rest_client_patch.diff, scripts/client_go_retry_patch.diff, scripts/keepalive_patch.diff), this PR updates scripts/rest_client_patch.diff and re-syncs scripts/client_go_retry_patch.diff alongside kubernetes/client/rest.py so regenerations preserve the fix in the meantime.

I am also happy to open a matching upstream PR in OpenAPITools/openapi-generator against python/rest.mustache so the local hunk in rest_client_patch.diff can be removed once kubernetes-client/gen upgrades past it. Would you prefer landing the local patch here now (with an upstream PR in parallel), or waiting on the upstream generator release?

@yliaog

yliaog commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

thanks for the PR, we can merge it. In the mean time, could you please open a PR in the upstream?

/lgtm
/approve

@kubernetes-prow kubernetes-prow Bot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Nov 21, 2026
@kubernetes-prow

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: eisen0419, yliaog

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

The pull request process is described 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 approved Indicates a PR has been approved by an approver from all required OWNERS files. label Nov 21, 2026
@kubernetes-prow
kubernetes-prow Bot merged commit 2a39678 into kubernetes-client:master Nov 21, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. 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. lgtm "Looks good to me", indicates that a PR is ready to be merged. 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

2 participants