From a31c9b7c7d130a46af6de6e1c13e83f6bee7b272 Mon Sep 17 00:00:00 2001 From: Edward Andrews-Hodgson Date: Mon, 15 May 2017 20:44:12 +0100 Subject: [PATCH] Save full response to a file (#207) * Save full response to a file * Add a new button on the response preview pane * Save full response to file when button clicked * Update after PR comments * It's a Response, not a Request * Remove file extension requirement --- .../dropdowns/preview-mode-dropdown.js | 9 +++- app/ui/components/response-pane.js | 43 ++++++++++++++++++- 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/app/ui/components/dropdowns/preview-mode-dropdown.js b/app/ui/components/dropdowns/preview-mode-dropdown.js index 58f4209b5..2a0c166b2 100644 --- a/app/ui/components/dropdowns/preview-mode-dropdown.js +++ b/app/ui/components/dropdowns/preview-mode-dropdown.js @@ -22,7 +22,7 @@ class PreviewModeDropdown extends PureComponent { } render () { - const {download} = this.props; + const {download, fullDownload} = this.props; return ( @@ -33,7 +33,11 @@ class PreviewModeDropdown extends PureComponent { Actions - Save to File + Save Response Body + + + + Save Full Response ); @@ -44,6 +48,7 @@ PreviewModeDropdown.propTypes = { // Functions updatePreviewMode: PropTypes.func.isRequired, download: PropTypes.func.isRequired, + fullDownload: PropTypes.func.isRequired, // Required previewMode: PropTypes.string.isRequired diff --git a/app/ui/components/response-pane.js b/app/ui/components/response-pane.js index ea59d4865..a00c37058 100644 --- a/app/ui/components/response-pane.js +++ b/app/ui/components/response-pane.js @@ -57,7 +57,7 @@ class ResponsePane extends PureComponent { const extension = mime.extension(contentType) || ''; const options = { - title: 'Save Response', + title: 'Save Response Body', buttonLabel: 'Save', filters: [{ name: 'Download', extensions: [extension] @@ -81,6 +81,46 @@ class ResponsePane extends PureComponent { }); } + async _handleDownloadFullResponseBody () { + if (!this.state.response) { + // Should never happen + console.warn('No response to download'); + return; + } + + const {body, timeline, encoding} = this.state.response; + const headers = timeline + .filter(v => v.name === 'HEADER_IN') + .map(v => v.value) + .join(''); + const bodyBuffer = new Buffer(body, encoding); + const fullResponse = `${headers}${bodyBuffer}`; + + const options = { + title: 'Save Full Response', + buttonLabel: 'Save', + filters: [{ + name: 'Download' + }] + }; + + remote.dialog.showSaveDialog(options, filename => { + if (!filename) { + trackEvent('Response', 'Save Full Cancel'); + return; + } + + fs.writeFile(filename, fullResponse, {}, err => { + if (err) { + console.warn('Failed to save full response', err); + trackEvent('Response', 'Save Full Failure'); + } else { + trackEvent('Response', 'Save Full Success'); + } + }); + }); + } + componentWillReceiveProps (nextProps) { const activeRequestId = nextProps.request ? nextProps.request._id : null; const activeResponseId = nextProps.activeResponseId; @@ -202,6 +242,7 @@ class ResponsePane extends PureComponent {