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

Add options to customize parsing/stringifying JSON #1298

Merged
merged 23 commits into from
Jun 5, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 21 additions & 9 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -326,13 +326,18 @@ Function used to parse JSON responses.

Example:
Giotino marked this conversation as resolved.
Show resolved Hide resolved

Using Bourne to prevent prototype pollution
Giotino marked this conversation as resolved.
Show resolved Hide resolved
```js
Giotino marked this conversation as resolved.
Show resolved Hide resolved
const got = require('got');
const Bourne = require('@hapi/bourne');

const parsed = await got('https://example.com', {
parseJson: text => Bourne.parse(text)
}).json();
(async () => {
const parsed = await got('https://example.com', {
parseJson: text => Bourne.parse(text)
}).json();

console.log(parsed);
})();
```

###### stringifyJson
Expand All @@ -344,15 +349,22 @@ Function used to stringify JSON requests body.

Example:

All numbers as strings
Giotino marked this conversation as resolved.
Show resolved Hide resolved
```js
const got = require('got');

await got.post('https://example.com', {
stringifyJson: object => JSON.stringify(object),
json: {
some: 'payload'
}
});
(async () => {
await got.post('https://example.com', {
stringifyJson: object => JSON.stringify(object, (key, value) => {
if (typeof value === "number") return value.toString();
return value;
}),
json: {
some: 'payload',
number: 1
}
});
})();
```

###### resolveBodyOnly
Expand Down