Skip to content

Commit 1ee7bd8

Browse files
Merge pull request #309 from microsoftgraph/JS-Issue-293-298-Emptybody-Content-type
Js issue 293 298 emptybody content type
2 parents ed902ec + 62b5f8c commit 1ee7bd8

File tree

4 files changed

+57
-26
lines changed

4 files changed

+57
-26
lines changed

spec/core/GraphRequestUtil.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,10 @@ describe("GraphRequestUtil.ts", () => {
7171
const val = undefined;
7272
assert.equal(serializeContent(val), undefined);
7373
});
74+
75+
it("Should return 'null' for the case of null content value", () => {
76+
const val = null;
77+
assert.equal(serializeContent(val), "null");
78+
});
7479
});
7580
});

spec/development/workload/OneNote.ts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,19 +70,27 @@ describe("OneNote", function() {
7070
}
7171
});
7272
it("Create a OneNote page with html page content", async () => {
73-
try {
74-
const formData = new FormData();
75-
formData.append("Presentation", fs.createReadStream("./spec/sample_files/onenotepage.html"));
76-
const json = await client.api(`/me/onenote/sections/${section.id}/pages`).post(formData);
77-
const createdPageFromHTML = json as OnenotePage;
73+
const formData = new FormData();
74+
formData.append("Presentation", fs.createReadStream("./spec/sample_files/onenotepage.html"));
75+
const json = await client.api(`/me/onenote/sections/${section.id}/pages`).post(formData);
76+
const createdPageFromHTML = json as OnenotePage;
7877

79-
assert.isDefined(createdPage.id);
80-
assert.isDefined(createdPage.contentUrl);
81-
assert.equal("New Page", createdPageFromHTML.title);
82-
assert.isUndefined(createdPage["random fake property that should be null"]);
83-
} catch (error) {
84-
throw error;
85-
}
78+
assert.isDefined(createdPage.id);
79+
assert.isDefined(createdPage.contentUrl);
80+
assert.equal("New Page", createdPageFromHTML.title);
81+
assert.isUndefined(createdPage["random fake property that should be null"]);
82+
});
83+
84+
it("Create a OneNote page with application/xhtml+xml page content", async () => {
85+
const body = "<!DOCTYPE html><html><head><title>A page with a block of HTML</title></head><body><p>This page contains some <i>formatted</i> <b>text</b>.</p></body></html>";
86+
const json = await client
87+
.api(`/me/onenote/sections/${section.id}/pages`)
88+
.header("content-type", "application/xhtml+xml")
89+
.post(body);
90+
const createdPageFromHTML = json as OnenotePage;
91+
assert.isDefined(createdPage.id);
92+
assert.isDefined(createdPage.contentUrl);
93+
assert.isUndefined(createdPage["random fake property that should be null"]);
8694
});
8795

8896
it("create a OneNote page with html page content and file attachment", async () => {

src/GraphRequest.ts

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import { MiddlewareControl } from "./middleware/MiddlewareControl";
2222
import { MiddlewareOptions } from "./middleware/options/IMiddlewareOptions";
2323
import { RequestMethod } from "./RequestMethod";
2424
import { ResponseType } from "./ResponseType";
25-
2625
/**
2726
* @interface
2827
* Signature to representing key value pairs
@@ -393,6 +392,27 @@ export class GraphRequest {
393392
}
394393
}
395394

395+
/**
396+
* @private
397+
* Checks if the content-type is present in the _headers property. If not present, defaults the content-type to application/json
398+
* @param none
399+
* @returns nothing
400+
*/
401+
private setHeaderContentType(): void {
402+
if (!this._headers) {
403+
this.header("Content-Type", "application/json");
404+
return;
405+
}
406+
const headerKeys = Object.keys(this._headers);
407+
for (const headerKey of headerKeys) {
408+
if (headerKey.toLowerCase() === "content-type") {
409+
return;
410+
}
411+
}
412+
// Default the content-type to application/json in case the content-type is not present in the header
413+
this.header("Content-Type", "application/json");
414+
}
415+
396416
/**
397417
* @public
398418
* Sets the custom header for a request
@@ -632,13 +652,15 @@ export class GraphRequest {
632652
const options: FetchOptions = {
633653
method: RequestMethod.POST,
634654
body: serializeContent(content),
635-
headers:
636-
typeof FormData !== "undefined" && content instanceof FormData
637-
? {}
638-
: {
639-
"Content-Type": "application/json",
640-
},
641655
};
656+
const className: string = content === undefined || content === null ? undefined : content.constructor.name;
657+
if (className === "FormData") {
658+
// Content-Type headers should not be specified in case the of FormData type content
659+
options.headers = {};
660+
} else {
661+
this.setHeaderContentType();
662+
options.headers = this._headers;
663+
}
642664
try {
643665
const response = await this.send(url, options, callback);
644666
return response;
@@ -673,12 +695,10 @@ export class GraphRequest {
673695
*/
674696
public async put(content: any, callback?: GraphRequestCallback): Promise<any> {
675697
const url = this.buildFullUrl();
698+
this.setHeaderContentType();
676699
const options: FetchOptions = {
677700
method: RequestMethod.PUT,
678701
body: serializeContent(content),
679-
headers: {
680-
"Content-Type": "application/json",
681-
},
682702
};
683703
try {
684704
const response = await this.send(url, options, callback);
@@ -698,12 +718,10 @@ export class GraphRequest {
698718
*/
699719
public async patch(content: any, callback?: GraphRequestCallback): Promise<any> {
700720
const url = this.buildFullUrl();
721+
this.setHeaderContentType();
701722
const options: FetchOptions = {
702723
method: RequestMethod.PATCH,
703724
body: serializeContent(content),
704-
headers: {
705-
"Content-Type": "application/json",
706-
},
707725
};
708726
try {
709727
const response = await this.send(url, options, callback);

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)