Skip to content

Commit c66a710

Browse files
authored
Convert form-url encoded content-type to json & use qs parser (#185)
* Convert content-type to json & use qs parser - `Utils.safeJSONParse` uses `JSON.parse` under the hood which cannot convert form url encoded query strings (which is what the string format of the binary data/Buffer string would be) to a "JSON Object" so we'll use the core node.js module, `querystring` to parse it. However, this parser might not automagically convert the Buffer to a string, so we'll convert it first and then parse it using the correct parser. - For some reason, we need to have the `content-type` to use `application/json`. This is no matter if the body is correctly parsed or not. My guess is there is some method in `hydra` library that requires the header content type to be json rather or the body is parsed in a string format rather than a JSON format. This change seems to be the cleanest with minimal changes to the surface area without also updating any other libraries. Previously, we are unable to send any requests with form url encoded body params whereas now form url encoded body params will be converted to json body params. * Minor bump version from 1.6.20 -> 1.7.0 * Remove speculated comment
1 parent 33387bb commit c66a710

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

lib/servicerouter.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -779,7 +779,16 @@ class ServiceRouter {
779779
headers: Object.assign({}, request.headers)
780780
};
781781

782-
message.body = Utils.safeJSONParse(body) || {};
782+
if (request.headers['content-type'] === 'application/x-www-form-urlencoded') {
783+
message.headers['content-type'] = 'application/json';
784+
try {
785+
message.body = querystring.parse(body.toString());
786+
} catch (e) {
787+
message.body = {};
788+
}
789+
} else {
790+
message.body = Utils.safeJSONParse(body) || {};
791+
}
783792

784793
if (request.headers['authorization']) {
785794
message.authorization = request.headers['authorization'];

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "hydra-router",
3-
"version": "1.6.20",
3+
"version": "1.7.0",
44
"description": "A service which routes requests to hydra-based microservices",
55
"author": {
66
"name": "Carlos Justiniano",

0 commit comments

Comments
 (0)