Skip to content

Commit

Permalink
Handle JSON protection prefix when parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeembrey committed Jul 10, 2016
1 parent bcc9e29 commit 4cd1e2a
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import Request from './request'
export type TextTypes = 'text' | 'json' | 'urlencoded'
export const textTypes = ['text', 'json', 'urlencoded']

const PROTECTION_PREFIX = /^\)\]\}',?\n/

export function parse (request: Request, value: string, type: string) {
// Return plain-text as is.
if (type === 'text') {
Expand All @@ -19,7 +21,7 @@ export function parse (request: Request, value: string, type: string) {
// Attempt to parse the response as JSON.
if (type === 'json') {
try {
return JSON.parse(value)
return JSON.parse(value.replace(PROTECTION_PREFIX, ''))
} catch (err) {
throw request.error(`Unable to parse response body: ${err.message}`, 'EPARSE', err)
}
Expand Down

0 comments on commit 4cd1e2a

Please sign in to comment.