Skip to content

Commit

Permalink
fix #14
Browse files Browse the repository at this point in the history
  • Loading branch information
Huachao committed Jul 19, 2016
1 parent 03bc69b commit 3e8b2af
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/views/httpResponseTextDocumentContentProvider.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";

import { TextDocumentContentProvider, EventEmitter, Event, Uri } from 'vscode';
import { TextDocumentContentProvider, EventEmitter, Event, Uri, window } from 'vscode';
import { HttpResponse } from '../models/httpResponse'
import { MimeUtility } from '../mimeUtility'

Expand Down Expand Up @@ -66,7 +66,11 @@ ${HttpResponseTextDocumentContentProvider.formatBody(this.response.body, this.re
if (contentType) {
let type = MimeUtility.parse(contentType).type;
if (type === 'application/json') {
body = JSON.stringify(JSON.parse(body), null, 4);
if (HttpResponseTextDocumentContentProvider.isJsonString(body)) {
body = JSON.stringify(JSON.parse(body), null, 4);
} else {
window.showWarningMessage('The content type of response is application/json, while response body is not a valid json string');
}
} else if (type === 'application/xml') {
body = pd.xml(body);
}
Expand All @@ -80,4 +84,13 @@ ${HttpResponseTextDocumentContentProvider.formatBody(this.response.body, this.re
return HttpResponseTextDocumentContentProvider._tagsToReplace[tag] || tag;
});
}

private static isJsonString(data: string) {
try {
JSON.parse(data);
return true;
} catch (e) {
return false;
}
}
}

0 comments on commit 3e8b2af

Please sign in to comment.