Skip to content

fix(http-client-js): serialize form-urlencoded bodies with URLSearchParams - #12412

Open
Joel Town Road (hippi345) wants to merge 1 commit into
microsoft:mainfrom
hippi345:fix-12349-form-urlencoded
Open

Joel Town Road (hippi345) wants to merge 1 commit into
microsoft:mainfrom
hippi345:fix-12349-form-urlencoded

Conversation

@hippi345

Copy link
Copy Markdown

Summary

Fixes #12349.

@typespec/http-client-js sent application/x-www-form-urlencoded request bodies through the JSON transport transform, so the runtime posted JSON under a form content type. Those model and record bodies are now encoded with URLSearchParams.

The generated encodeFormUrlEncoded helper skips null and undefined values, repeats keys for arrays, and JSON-encodes nested objects. Emitter scenarios cover a Record<string> body and a model body.

Test plan

  • pnpm --filter @typespec/http-client-js build
  • pnpm --filter @typespec/http-client-js test
  • pnpm --filter @typespec/http-client-js lint

…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

Copy link
Copy Markdown
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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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 URLSearchParams helper 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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

emitter:client:js Issue for the JS client emitter: @typespec/http-client-js

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: http-client-js sends application/x-www-form-urlencoded request bodies as JSON

2 participants