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
This commit is contained in:
Edward Andrews-Hodgson 2017-05-15 20:44:12 +01:00 committed by Gregory Schier
parent 0e6f7750e8
commit a31c9b7c7d
2 changed files with 49 additions and 3 deletions

View File

@ -22,7 +22,7 @@ class PreviewModeDropdown extends PureComponent {
}
render () {
const {download} = this.props;
const {download, fullDownload} = this.props;
return (
<Dropdown>
<DropdownButton className="tall">
@ -33,7 +33,11 @@ class PreviewModeDropdown extends PureComponent {
<DropdownDivider>Actions</DropdownDivider>
<DropdownItem onClick={download}>
<i className="fa fa-save"/>
Save to File
Save Response Body
</DropdownItem>
<DropdownItem onClick={fullDownload}>
<i className="fa fa-save" />
Save Full Response
</DropdownItem>
</Dropdown>
);
@ -44,6 +48,7 @@ PreviewModeDropdown.propTypes = {
// Functions
updatePreviewMode: PropTypes.func.isRequired,
download: PropTypes.func.isRequired,
fullDownload: PropTypes.func.isRequired,
// Required
previewMode: PropTypes.string.isRequired

View File

@ -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 {
</Button>
<PreviewModeDropdown
download={this._handleDownloadResponseBody}
fullDownload={this._handleDownloadFullResponseBody}
previewMode={previewMode}
updatePreviewMode={handleSetPreviewMode}
/>