Skip to content

fix: propagate Content-Length header to upstream requests#2375

Open
octo-patch wants to merge 1 commit into
songquanpeng:mainfrom
octo-patch:fix/issue-2366-content-length-header
Open

fix: propagate Content-Length header to upstream requests#2375
octo-patch wants to merge 1 commit into
songquanpeng:mainfrom
octo-patch:fix/issue-2366-content-length-header

Conversation

@octo-patch
Copy link
Copy Markdown

Fixes #2366

Problem

When the request body is passed through unchanged (OpenAI-compatible pass-through mode, e.g., for Azure channels with no format conversion needed), Go's http.NewRequest receives a generic io.Reader and cannot determine the content length. It sets ContentLength to -1, which causes the Go HTTP client to send the request with Transfer-Encoding: chunked instead of a Content-Length header.

Some upstream APIs — specifically Azure's grok model — require an explicit Content-Length header and reject chunked requests with:

{"error":{"message":"Content-Length header is required to make request."}}

Solution

In DoRequestHelper, after creating the upstream *http.Request, check if ContentLength is still -1 (unknown). If so, inherit it from the original client request (c.Request.ContentLength).

This fallback applies only to the pass-through case. When the request body has been re-serialized via ConvertRequest (into a *bytes.Buffer), Go's http.NewRequest already computes the length automatically and ContentLength will be non-negative — the fallback is not applied.

This mirrors the existing pattern in relay/controller/audio.go where req.ContentLength = c.Request.ContentLength is set for Azure audio requests.

Testing

  • Verified the fix targets the same code path that the audio controller already handles correctly.
  • The change is minimal and does not affect re-serialized requests (where ContentLength is already set by Go).

…quanpeng#2366)

When the request body is passed through unchanged (e.g., OpenAI-compatible
pass-through mode), Go's http.NewRequest cannot determine the content length
and sets ContentLength to -1, resulting in chunked transfer encoding.

Some upstream APIs such as Azure's grok model require an explicit
Content-Length header and reject chunked requests with a 400 error.

Fix: after creating the upstream request, if ContentLength is still -1,
inherit it from the original client request. This correctly handles the
pass-through case. When the body has been re-serialized (e.g., after
ConvertRequest), Go already computes the length from bytes.Buffer and
this fallback is not applied.
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.

你们使用grok model ,没有碰到 {"error":{"message":"Content-Length header is required to make request. 错误吗?

1 participant