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

pretty print json on clicking fetch data button #137

Merged
merged 2 commits into from
Jul 5, 2018

Conversation

amitmbee
Copy link
Contributor

@amitmbee amitmbee commented Jul 3, 2018

No description provided.

@@ -29,7 +29,9 @@ const fetchDataAlias = (payload: payload) => {
requestHeadersObject
})
.then((data: axios.AxiosResponse) => {
const stringifiedData = JSON.stringify(data.data);
const stringifiedData = data.headers["content-type"].includes("json")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why we need this condition?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we dont need the pretty printing if the content-type is anything other than application/json. I had earlier added a strict equals condition for checking, but turns out that some websites sent out content-type headers as application/json; charset=utf-8. So this condition is needed to filter out types like text/html/text/plain or application/x-www-form-urlencoded for instance

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okey. but in the false part of this condition we are doing JSON.stringify doesn't that break for text/html?

Copy link
Contributor Author

@amitmbee amitmbee Jul 4, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is quite strange. I logged out the following

bug-json

and intercepted a svg file, for which the output is:

bug--json

running the following on chrome console or node repl gives me the following error

chrome-console

but we are getting the proper output for stringified data as you can see in first screenshot instead of it throwing an error. Not sure what is happening here.

Edit: Also note that the actions are getting dispatched with the proper payload and in proper order

screen shot 2018-07-04 at 4 26 34 pm

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what happens if I don't stringify the data for svg type (and what should happen ideally)

screen shot 2018-07-04 at 4 30 25 pm

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean without JSON.stringify does the correct data coming to textbox?
like without escape?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, for non-json types, correct data is coming through. for json type, i am getting [object Object]

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

then why can't we remove the JSON.stringify for non JSON-types.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Please check

const stringifiedData = JSON.stringify(data.data);
const stringifiedData = data.headers["content-type"].includes("json")
? JSON.stringify(data.data, null, 2)
: JSON.stringify(data.data);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this will create issue when the response is html/xml?

@revathskumar revathskumar merged commit 7f5daba into master Jul 5, 2018
@revathskumar revathskumar deleted the pretty-print-json branch July 5, 2018 07:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants