Skip to content

Commit 0443c23

Browse files
authored
fix: send content-type as application/json when necessary
2 parents 9dd3004 + 7704d30 commit 0443c23

File tree

4 files changed

+30
-3
lines changed

4 files changed

+30
-3
lines changed

src/content-types/collection/manager.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,13 @@ export class CollectionTypeManager {
152152
url = URLHelper.appendQueryParams(url, queryParams);
153153
}
154154

155-
const response = await this._httpClient.post(url, JSON.stringify({ data }));
155+
const response = await this._httpClient.post(
156+
url,
157+
// Wrap the payload in a data object
158+
JSON.stringify({ data }),
159+
// By default POST requests sets the content-type to text/plain
160+
{ headers: { 'Content-Type': 'application/json' } }
161+
);
156162

157163
debug('created the %o document', this._pluralName);
158164

@@ -198,7 +204,13 @@ export class CollectionTypeManager {
198204
url = URLHelper.appendQueryParams(url, queryParams);
199205
}
200206

201-
const response = await this._httpClient.put(url, JSON.stringify({ data }));
207+
const response = await this._httpClient.put(
208+
url,
209+
// Wrap the payload in a data object
210+
JSON.stringify({ data }),
211+
// By default PUT requests sets the content-type to text/plain
212+
{ headers: { 'Content-Type': 'application/json' } }
213+
);
202214

203215
debug('updated the %o document with id %o', this._pluralName, documentID);
204216

src/content-types/single/manager.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,13 @@ export class SingleTypeManager {
113113
url = URLHelper.appendQueryParams(url, queryParams);
114114
}
115115

116-
const response = await this._httpClient.put(url, JSON.stringify({ data }));
116+
const response = await this._httpClient.put(
117+
url,
118+
// Wrap the payload in a data object
119+
JSON.stringify({ data }),
120+
// By default PUT requests sets the content-type to text/plain
121+
{ headers: { 'Content-Type': 'application/json' } }
122+
);
117123

118124
debug('the %o document has been updated', this._singularName);
119125

tests/unit/content-types/collection/collection-manager.test.ts

+6
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ describe('CollectionTypeManager CRUD Methods', () => {
8484
expect(requestSpy).toHaveBeenCalledWith('/articles?locale=en', {
8585
method: 'POST',
8686
body: JSON.stringify({ data: payload }),
87+
headers: {
88+
'Content-Type': 'application/json',
89+
},
8790
});
8891
});
8992

@@ -99,6 +102,9 @@ describe('CollectionTypeManager CRUD Methods', () => {
99102
expect(requestSpy).toHaveBeenCalledWith('/articles/1?locale=en', {
100103
method: 'PUT',
101104
body: JSON.stringify({ data: payload }),
105+
headers: {
106+
'Content-Type': 'application/json',
107+
},
102108
});
103109
});
104110

tests/unit/content-types/collection/single-manager.test.ts

+3
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ describe('SingleTypeManager CRUD Methods', () => {
5656
expect(requestSpy).toHaveBeenCalledWith('/homepage?locale=en', {
5757
method: 'PUT',
5858
body: JSON.stringify({ data: payload }),
59+
headers: {
60+
'Content-Type': 'application/json',
61+
},
5962
});
6063
});
6164

0 commit comments

Comments
 (0)