Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: continue replacing application/x-www-form-urlencoded with application/json #263

Merged
merged 4 commits into from
Mar 24, 2020
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
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