Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions sdk/core/ts-http-runtime/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

### Bugs Fixed

- Fixed an issue where setting `content-type` header was ignored when the request has no body. [#37181](https://github.com/Azure/azure-sdk-for-js/pull/37181)

### Other Changes

## 0.3.2 (2025-11-06)
Expand Down
14 changes: 8 additions & 6 deletions sdk/core/ts-http-runtime/src/client/sendRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export async function sendRequest(
* @param options - request options InternalRequestParameters
* @returns returns the content-type
*/
function getRequestContentType(options: InternalRequestParameters = {}): string {
function getRequestContentType(options: InternalRequestParameters = {}): string | undefined {
return (
options.contentType ??
(options.headers?.["content-type"] as string) ??
Expand All @@ -88,6 +88,10 @@ function getRequestContentType(options: InternalRequestParameters = {}): string
* @returns returns the content-type
*/
function getContentType(body: any): string | undefined {
if (body === undefined) {
return undefined;
}

if (ArrayBuffer.isView(body)) {
return "application/octet-stream";
}
Expand Down Expand Up @@ -116,15 +120,13 @@ function buildPipelineRequest(
): PipelineRequest {
const requestContentType = getRequestContentType(options);
const { body, multipartBody } = getRequestBody(options.body, requestContentType);
Comment thread
timovv marked this conversation as resolved.
const hasContent = body !== undefined || multipartBody !== undefined;

const headers = createHttpHeaders({
...(options.headers ? options.headers : {}),
accept: options.accept ?? options.headers?.accept ?? "application/json",
...(hasContent &&
requestContentType && {
"content-type": requestContentType,
}),
...(requestContentType && {
"content-type": requestContentType,
}),
});

return createPipelineRequest({
Expand Down
16 changes: 14 additions & 2 deletions sdk/core/ts-http-runtime/test/client/sendRequest.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,16 +366,28 @@ describe("sendRequest", () => {
await sendRequest("POST", mockBaseUrl, mockPipeline, { contentType: "testContent", body: {} });
});

it("should not set content-type if no body is present", async () => {
it("should set content-type via contentType option even without body", async () => {
Comment thread
timovv marked this conversation as resolved.
const mockPipeline: Pipeline = createEmptyPipeline();
mockPipeline.sendRequest = async (_client, request) => {
assert.equal(request.headers.get("content-type"), undefined);
assert.equal(request.headers.get("content-type"), "testContent");
return { headers: createHttpHeaders() } as PipelineResponse;
};

await sendRequest("POST", mockBaseUrl, mockPipeline, { contentType: "testContent" });
});

it("should set content-type via headers option even without body", async () => {
const mockPipeline: Pipeline = createEmptyPipeline();
mockPipeline.sendRequest = async (_client, request) => {
assert.equal(request.headers.get("content-type"), "application/xml");
return { headers: createHttpHeaders() } as PipelineResponse;
};

await sendRequest("POST", mockBaseUrl, mockPipeline, {
headers: { "content-type": "application/xml" },
});
});

it("should set custom accept", async () => {
const mockPipeline: Pipeline = createEmptyPipeline();
mockPipeline.sendRequest = async (_client, request) => {
Expand Down