Repository navigation
fix(http-client-js): serialize form-urlencoded bodies with URLSearchParams - #12412
Open
Joel Town Road (hippi345) wants to merge 1 commit into
Open
Joel Town Road (hippi345) wants to merge 1 commit into
Joel Town Road (hippi345) wants to merge 1 commit into
Conversation
…arams Request bodies with content type application/x-www-form-urlencoded were sent through the JSON transport transform. Encode those model and record bodies with URLSearchParams instead.
|
Azure Pipelines: Successfully started running 1 pipeline(s). 1 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Nullable form bodies still bypass encoding, and form-specific field names are mishandled for named models and parameterized content types.
3 open issues
What changed in this PR
Fixes #12349 by adding URL-encoded request-body serialization to the JavaScript HTTP client emitter instead of sending JSON under a form content type.
Changes:
- Adds a conditional
URLSearchParamshelper that skips absent values, repeats array keys, and JSON-encodes nested objects. - Routes model and record form bodies through the helper.
- Adds emitter scenarios and a changelog entry.
| File | Description |
|---|---|
packages/http-client-js/test/scenarios/http-operations/form-urlencoded-body.md |
Adds record and model form-body scenarios. |
packages/http-client-js/src/utils/form-urlencoded.ts |
Detects form content types, including parameters. |
packages/http-client-js/src/emitter.tsx |
Registers conditional form-helper emission. |
packages/http-client-js/src/components/transforms/operation-transform-expression.tsx |
Wraps form-body transforms with the encoder. |
packages/http-client-js/src/components/static-helpers/form-urlencoded.tsx |
Generates the form encoder when needed. |
packages/http-client-js/src/components/index.ts |
Exports the helper component. |
.chronus/changes/fix-12349-form-urlencoded-2026-9-27-19-55-38.md |
Records the serialization fix. |
Review effort: Balanced
| const payloadType = body.type; | ||
| if ( | ||
| body.bodyKind === "single" && | ||
| payloadType.kind === "Model" && |
| <ContentTypeEncodingProvider contentType={contentType}> | ||
| <TransformNamePolicyContext.Provider | ||
| value={{ | ||
| getTransportName: (type) => defaultTransportNameGetter(type, contentType), |
| }} | ||
| > | ||
| {code`${getEncodeFormUrlEncodedRef()}(${( | ||
| <JsonTransform itemRef={itemRef} target="transport" type={payloadType} /> |
This branch has not been deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.

Summary
Fixes #12349.
@typespec/http-client-jssentapplication/x-www-form-urlencodedrequest bodies through the JSON transport transform, so the runtime posted JSON under a form content type. Those model and record bodies are now encoded withURLSearchParams.The generated
encodeFormUrlEncodedhelper skips null and undefined values, repeats keys for arrays, and JSON-encodes nested objects. Emitter scenarios cover aRecord<string>body and a model body.Test plan
pnpm --filter @typespec/http-client-js buildpnpm --filter @typespec/http-client-js testpnpm --filter @typespec/http-client-js lint