-
-
Save adriancuadrado/84408b9b6d53e41858a2f1b9f20090d6 to your computer and use it in GitHub Desktop.
(async function() { | |
const filename = 'postman-history-export.json'; | |
const download = (function() { | |
const element = document.createElement('a'); | |
return function(filename, contents) { | |
element.setAttribute('href', URL.createObjectURL(new Blob([contents]))); | |
element.setAttribute('download', filename); | |
element.click(); | |
} | |
}()); | |
let transaction = await new Promise( | |
resolve => | |
indexedDB | |
.open('postman-app') | |
.onsuccess = | |
event => | |
resolve( | |
event | |
.target | |
.result | |
.transaction(['history', 'history_responses']) | |
) | |
); | |
function getAll(transaction, objectStore) { | |
return new Promise( | |
resolve => | |
transaction | |
.objectStore(objectStore) | |
.getAll() | |
.onsuccess = | |
event => | |
resolve(event.target.result) | |
); | |
} | |
const data = { | |
requests: await getAll(transaction, 'history'), | |
responses: await getAll(transaction, 'history_responses') | |
}; | |
download(filename, JSON.stringify(data)); | |
})(); |
(async function() { | |
function selectFile () { | |
let resolveFile = null; | |
const inputElement = document.createElement('input'); | |
inputElement.setAttribute('type', 'file'); | |
inputElement.addEventListener('change', event => resolveFile(event.target.files[0])); | |
inputElement.click(); | |
return new Promise(r => resolveFile = r); | |
} | |
const getFileContents = (function () { | |
let resolveContents = null; | |
const reader = new FileReader(); | |
reader.addEventListener('load', event => resolveContents(event.target.result)); | |
return function (file) { | |
reader.readAsText(file); | |
return new Promise(r => resolveContents = r); | |
} | |
})(); | |
async function importData(data) { | |
let transaction = await new Promise( | |
resolve => | |
indexedDB | |
.open('postman-app') | |
.onsuccess = | |
event => | |
resolve( | |
event | |
.target | |
.result | |
.transaction( | |
[ | |
'history', | |
'history_responses' | |
], 'readwrite' | |
) | |
) | |
); | |
function addAll(objectStore, data) { | |
objectStore.clear().onsuccess = () => data.forEach(e => objectStore.add(e)); | |
} | |
addAll(transaction.objectStore('history'), data.requests); | |
addAll(transaction.objectStore('history_responses'), data.responses); | |
} | |
importData(JSON.parse(await getFileContents(await selectFile()))); | |
})(); |
await (async function() { | |
// These are the properties to keep for each request and response. The database returns more properties that are not | |
// interesting. If you remove all items from the `request` and `response` arrays in the `propertiesToKeep` object, the | |
// request and response objects returned will contain all properties from the database instead of just these. | |
let propertiesToKeep = { | |
request: [ | |
'createdAt', | |
'headerData', | |
'method', | |
'data', | |
'response', | |
'url' | |
], | |
response: [ | |
'cookies', | |
'headers', | |
'responseCode', | |
'text' | |
] | |
} | |
let transaction = await new Promise( | |
resolve => | |
indexedDB | |
.open('postman-app') | |
.onsuccess = | |
event => | |
resolve( | |
event | |
.target | |
.result | |
.transaction(['history', 'history_responses']) | |
) | |
); | |
function getAll(transaction, objectStore) { | |
return new Promise( | |
resolve => | |
transaction | |
.objectStore(objectStore) | |
.getAll() | |
.onsuccess = | |
event => | |
resolve(event.target.result) | |
); | |
} | |
let requests = (await getAll(transaction, 'history')) | |
.sort((r1, r2) => r1.createdAt.localeCompare(r2.createdAt)); | |
let responses = await getAll(transaction, 'history_responses'); | |
requests.forEach(req => { | |
let res = responses.find(r => r.history == req.id); | |
req.response = res; | |
if(propertiesToKeep.request.length > 0) { | |
Object.keys(req).filter(k => !propertiesToKeep.request .includes(k)).forEach(k => delete req[k]); | |
} | |
if(propertiesToKeep.response.length > 0){ | |
Object.keys(res).filter(k => !propertiesToKeep.response.includes(k)).forEach(k => delete res[k]); | |
} | |
}); | |
return requests; | |
})(); |
.[] as $req | | |
"curl \\\n" | |
+ "--location \\\n" | |
+ "--request " + $req.method + " \\\n" | |
+ if | |
$req.method != "GET" | |
and $req.method != "HEAD" | |
and $req.method != "DELETE" | |
then "--data-raw '" + $req.data + "' \\\n" | |
else "" | |
end | |
+ if $req.headerData | |
then | |
reduce ( | |
$req.headerData[] | |
| "--header '" + .key + ": " + .value + "' \\\n" | |
) as $line (""; . + $line) | |
else "" | |
end | |
+ "'" + $req.url + "'" + "\n" |
@mrjoesm I ran the code and it worked for me. Make sure you select Show DevTools (Current View) instead of Show DevTools (Current Shell) under View → Developer. Also, my Postman version is 9.22.2. Which is yours? You can check the version in the Gear icon in the top right corner → Release Notes.
Also I updated the comment to remove the code and include a reference to a Github gist where people can say what problems they have and make suggestions if they want. Please consider using that gist for anything related to the scripts I created instead of this thread. I don't want to overload this thread with more comments about my code.
@adriancuadrado v9.24.1 for me in (settings > about)
interestingly (?), your dev tools specifically references the path vs mine which is referring to desktop.postman.com/ etc etc
@mrjoesm I made sure to select Help → Check for Updates before reproducing this error and the update I got was 9.22.2. I'll try reinstalling Postman from the website.
Is your OS Windows by chance? Or some Linux distro?
@mrjoesm I just downloaded Postman again and the latest version is v9.23.3. Where and how did you get v9.24.1?
Windows 11
Are you signed in? I'm wondering if it's because I'm signed in and within a 'workspace' rather than not signed in and using a 'scratchpad' ?
You are up to date! Postman v9.24.1 is the latest version.
When I hit help > check for updates
Interestingly, I see 9.23.3 as a download too on their site 🤔
but v9.24 is in their release notes though https://www.postman.com/downloads/release-notes/
@mrjoesm I can reproduce the error. I think the problem is that the History is not saved locally when you log in into Postman. I'd bet it is saved in your Postman account instead. I'll have to study this a little more, but I think I'll have to see which requests Postman does to retrieve the History and make them.
TLDR: This shit is too hard and I think I won't keep working on your issue. Read the comment below to understand why and/or if you want to try fixing it yourself.
@mrjoesm I've been working on your issue for a while and I think I won't be able to make the import/export of history work in postman when you are logged in. I tried opening postman without my account in in one pc, and logging in with my postman account in another. Then, in the computer without my account, I set up the postman's proxy to view all requests and responses. In the computer where I logged in, I connected to the proxy. Then I restarted Postman to force it reload the requests from History so that I could find the request that gave me the History form my postman account. Finally, once the history is loaded, I disconnected the proxy and exported the requests that were made by postman itself to load the history from my account and tried to reproduce the requests. This last step is where I struggle, because no matter what I always get an error saying something like "Invalid Session ID". I think I have to replace the "sid" from the request with another value, but it doesn't work. I found the first request where the session id's value appears in the response and tried using that value, but I still get the same error.
Interestingly, when I use postman from the web browser (https://web.postman.co/), I get some differences in the way some requests are made. When I use the "Copy all as HAR" option in the Chrome devtools to see the requests being made by Chrome, it seems like the request that returns the History is actually a web socket. No idea why Postman doesn't show this in the History when I use the proxy. But it doesn't really matter because I couldn't reproduce the web socket response anyways.
If you ever try and find a solution to your issue, let me know and I'll update the Javascript code snippets accordingly.
thanks @adriancuadrado - I appreciate the effort
Copy the Javascript code you want in the Console tab of DevTools. This is how you open DevTools:
Important: Once you import the history, you have to close and reopen Postman to make it load the contents in the UI.
The
study_requests_and_responses.js
file doesn't create a file like the others. You have to copy its output to the clipboard. I did it on purpose because I thought it would be more convenient than having to select where to download the file and opening it over and over.Right click on the output of that code, select
Copy object
and paste it in your code editor like in the following image:The
curl_commands.jq
file is a jq filter that you run overstudy_requests_and_responses.js
's output to get the curl commands.You can either copy
study_requests_and_responses.js
's output in the JSON textbox of this jq sandbox, or run this command:jq --raw-output --from-file curl_commands.jq < output.json
The files have a number prefix to make them appear in the order I wanted because Github Gists orders files alphabetically.