Thanks to visit codestin.com
Credit goes to Github.com

Skip to content

http post/patch: --content-type flag is ignored for JSON bodies, always sends application/json #17640

@m3ac-AllbrittenJ

Description

@m3ac-AllbrittenJ

Description

When using http post or http patch with --content-type set to a JSON-variant type (e.g. application/json-patch+json), Nushell ignores the flag and always sends Content-Type: application/json; charset=utf-8 instead.

Steps to Reproduce

Start a local echo server (e.g. python3 -m http.server) or use any endpoint that reflects headers, then run:

import http.server, json

class Handler(http.server.BaseHTTPRequestHandler):
    def handle_request(self):
        length = int(self.headers.get("Content-Length", 0))
        body = self.rfile.read(length).decode()
        print(f"\n--- {self.command} {self.path}")
        print(f"Content-Type: {self.headers.get('Content-Type')}")
        print(f"Authorization: {self.headers.get('Authorization', '')[:20]}...")
        print(f"Body: {body}")
        self.send_response(200)
        self.send_header("Content-Type", "application/json")
        self.end_headers()
        self.wfile.write(b'{"id": 99999, "fields": {}}')

    def do_POST(self): self.handle_request()
    def do_PATCH(self): self.handle_request()
    def do_GET(self): self.handle_request()
    def log_message(self, *a): pass
let body = [{op: "add", path: "/foo", value: "bar"}]
http post "http://localhost:8000/test" --content-type "application/json-patch+json" ($body | to json)

Observe the actual Content-Type header received by the server.

Expected Behavior

The request is sent with Content-Type: application/json-patch+json as specified.

Actual Behavior

The request is sent with Content-Type: application/json; charset=utf-8, overriding the user-specified value.

This affects any API that requires a JSON-variant content type distinct from application/json, such as:

  • application/json-patch+json (RFC 6902 — JSON Patch)
  • application/merge-patch+json (RFC 7396)

Workaround

Embed the Content-Type header directly in the --headers list instead of using --content-type:

let headers = [Authorization "Bearer ..." "Content-Type" "application/json-patch+json"]
http post $url --headers $headers ($body | to json)

Environment

Nushell version: 0.110.0
OS: Linux (WSL2)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions