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
1 change: 1 addition & 0 deletions sdk/core/ts-http-runtime/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Features Added

- Added `allowCrossOriginRedirects` option to `RedirectPolicyOptions`. [#37384](https://github.com/Azure/azure-sdk-for-js/pull/37384)
- Support function type and `Blob` type for request body. ([#37300](https://github.com/Azure/azure-sdk-for-js/pull/37300), [#37424](https://github.com/Azure/azure-sdk-for-js/pull/37424))

### Breaking Changes

Expand Down
12 changes: 10 additions & 2 deletions sdk/core/ts-http-runtime/src/client/sendRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import type { Pipeline } from "../pipeline.js";
import { createHttpHeaders } from "../httpHeaders.js";
import { createPipelineRequest } from "../pipelineRequest.js";
import { getCachedDefaultHttpsClient } from "./clientHelpers.js";
import { isReadableStream } from "../util/typeGuards.js";
import { isBlob, isReadableStream } from "../util/typeGuards.js";
import type { HttpResponse, RequestParameters } from "./common.js";
import type { PartDescriptor } from "./multipart.js";
import { buildMultipartBody } from "./multipart.js";
Expand Down Expand Up @@ -96,6 +96,10 @@ function getContentType(body: any): string | undefined {
return "application/octet-stream";
}

if (isBlob(body) && body.type) {
return body.type;
}

if (typeof body === "string") {
try {
JSON.parse(body);
Expand Down Expand Up @@ -155,7 +159,7 @@ interface RequestBody {
/**
* Prepares the body before sending the request
*/
function getRequestBody(body?: unknown, contentType: string = ""): RequestBody {
export function getRequestBody(body?: unknown, contentType: string = ""): RequestBody {
if (body === undefined) {
return { body: undefined };
}
Expand All @@ -164,6 +168,10 @@ function getRequestBody(body?: unknown, contentType: string = ""): RequestBody {
return { body };
}

if (isBlob(body)) {
return { body };
}
Comment thread
jeremymeng marked this conversation as resolved.

if (isReadableStream(body) || typeof body === "function") {
return { body } as RequestBody;
}
Expand Down
32 changes: 32 additions & 0 deletions sdk/core/ts-http-runtime/test/browser/sendRequest.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import { describe, it, assert } from "vitest";
import { getRequestBody } from "../../src/client/sendRequest.js";

describe("getRequestBody (browser)", () => {
it("should handle FormData", () => {
const formData = new FormData();
formData.append("foo", "bar");
const result = getRequestBody(formData);
assert.strictEqual(result.body, formData);
});

it("should handle Blob", () => {
const blob = new Blob(["test content"], { type: "text/plain" });
Comment thread
jeremymeng marked this conversation as resolved.
const result = getRequestBody(blob);
assert.strictEqual(result.body, blob);
});

it("should handle Uint8Array", () => {
const array = new Uint8Array([1, 2, 3]);
const result = getRequestBody(array);
assert.strictEqual(result.body, array);
});

it("should handle ReadableStream", () => {
const stream = new ReadableStream();
const result = getRequestBody(stream);
assert.strictEqual(result.body, stream);
});
});
Loading