Skip to content

Commit

Permalink
fix: continue replacing application/x-www-form-urlencoded with applic…
Browse files Browse the repository at this point in the history
…ation/json (#263)
  • Loading branch information
bcoe authored Mar 24, 2020
1 parent a05f07f commit dca176d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/gaxios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,17 @@ export class Gaxios {
} else if (typeof opts.data === 'object') {
opts.body = JSON.stringify(opts.data);
// Allow the user to specifiy their own content type,
// such as application/json-patch+json:
if (!opts.headers['Content-Type']) {
// such as application/json-patch+json; for historical reasons this
// content type must currently be a json type, as we are relying on
// application/x-www-form-urlencoded (which is incompatible with
// upstream GCP APIs) being rewritten to application/json.
//
// TODO: refactor upstream dependencies to stop relying on this
// side-effect.
if (
!opts.headers['Content-Type'] ||
!opts.headers['Content-Type'].includes('json')
) {
opts.headers['Content-Type'] = 'application/json';
}
} else {
Expand Down
18 changes: 18 additions & 0 deletions test/test.getch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,24 @@ describe('🎏 data handling', () => {
assert.deepStrictEqual(res.data, {});
});

it('replaces application/x-www-form-urlencoded with application/json', async () => {
const body = {hello: '🌎'};
const scope = nock(url)
.matchHeader('Content-Type', 'application/json')
.post('/', JSON.stringify(body))
.reply(200, {});
const res = await request({
url,
method: 'POST',
data: body,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
});
scope.done();
assert.deepStrictEqual(res.data, {});
});

it('should return stream if asked nicely', async () => {
const body = {hello: '🌎'};
const scope = nock(url)
Expand Down

0 comments on commit dca176d

Please sign in to comment.