Skip to content

Commit 72adfd0

Browse files
committed
Adding a buildheaders function to conditionally set content type and checking if body is null when serializing content
1 parent 870477b commit 72adfd0

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

src/GraphRequest.ts

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,28 @@ export class GraphRequest {
302302
}
303303
}
304304

305+
/**
306+
* @private
307+
* Checks if the content-type is present in the _headers property. If not present, defaults the content-type to application/json
308+
* @param none
309+
* @returns nothing
310+
*/
311+
private buildHeaders(): void {
312+
if (this._headers === undefined || this._headers === null) {
313+
this.header("Content-Type", "application/json");
314+
}
315+
let isContentTypePresent = false;
316+
const headerKeys = Object.keys(this._headers);
317+
for (const headerKey of headerKeys) {
318+
if (headerKey.toLowerCase() === "content-type") {
319+
isContentTypePresent = true;
320+
}
321+
}
322+
if (!isContentTypePresent) {
323+
this.header("Content-Type", "application/json");
324+
}
325+
}
326+
305327
/**
306328
* @public
307329
* Sets the custom header for a request
@@ -550,13 +572,13 @@ export class GraphRequest {
550572
const options: FetchOptions = {
551573
method: RequestMethod.POST,
552574
body: serializeContent(content),
553-
headers:
554-
typeof FormData !== "undefined" && content instanceof FormData
555-
? {}
556-
: {
557-
"Content-Type": "application/json",
558-
},
559575
};
576+
if (typeof FormData !== "undefined" && content instanceof FormData) {
577+
options.headers = {};
578+
} else {
579+
this.buildHeaders();
580+
options.headers = this._headers;
581+
}
560582
try {
561583
const response = await this.send(url, options, callback);
562584
return response;

src/GraphRequestUtil.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export const urlJoin = (urlSegments: string[]): string => {
4141
*/
4242

4343
export const serializeContent = (content: any): any => {
44-
const className: string = content === undefined ? undefined : content.constructor.name;
44+
const className: string = content === undefined || content === null ? undefined : content.constructor.name;
4545
if (className === "Buffer" || className === "Blob" || className === "File" || className === "FormData" || typeof content === "string") {
4646
return content;
4747
}

0 commit comments

Comments
 (0)