Skip to content

Open-Source API Development Ecosystem • https://hoppscotch.io • Offline, On-Prem & Cloud • Web, Desktop & CLI • Open-Source Alternative to Postman, Insomnia

Postman collection parsing

Jacob Anaviscaccff68ad1807
diff --git a/components/collections/importExportCollections.vue b/components/collections/importExportCollections.vue
index 8df70d36d..6d79fb21e 100644
--- a/components/collections/importExportCollections.vue
+++ b/components/collections/importExportCollections.vue
@@ -120,20 +120,40 @@ export default {
reader.onload = event => {
let content = event.target.result;
let collections = JSON.parse(content);
- this.$store.commit("postwoman/replaceCollections", collections);
+ if (collections[0]) {
+ let [ name, folders, requests ] = Object.keys(collections[0])
+ if (name === 'name' && folders === 'folders' && requests === 'requests') {
+ // Do nothing
+ }
+ } else if (collections.info && collections.info.schema.includes('v2.1.0')) {
+ collections = this.parsePostmanCollection(collections)
+ } else {
+ return this.failedImport();
+ }
+ this.$store.commit("postwoman/importCollections", collections);
+ this.fileImported();
};
reader.readAsText(this.$refs.inputChooseFileToReplaceWith.files[0]);
- this.fileImported();
},
importFromJSON() {
let reader = new FileReader();
reader.onload = event => {
let content = event.target.result;
let collections = JSON.parse(content);
+ if (collections[0]) {
+ let [ name, folders, requests ] = Object.keys(collections[0])
+ if (name === 'name' && folders === 'folders' && requests === 'requests') {
+ // Do nothing
+ }
+ } else if (collections.info && collections.info.schema.includes('v2.1.0')) {
+ collections = this.parsePostmanCollection(collections);
+ } else {
+ return this.failedImport();
+ }
this.$store.commit("postwoman/importCollections", collections);
+ this.fileImported();
};
reader.readAsText(this.$refs.inputChooseFileToImportFrom.files[0]);
- this.fileImported();
},
exportJSON() {
let text = this.collectionJson;
@@ -161,6 +181,114 @@ export default {
this.$toast.info(this.$t("file_imported"), {
icon: "folder_shared"
});
+ },
+ failedImport() {
+ this.$toast.error(this.$t("import_failed"), {
+ icon: "error"
+ });
+ },
+ parsePostmanCollection(collection, folders = true) {
+ let postwomanCollection = folders ? [{
+ "name": "",
+ "folders": [],
+ "requests": []
+ }]
+ : {
+ "name": "",
+ "requests": []
+ };
+ for(let collectionItem of collection.item) {
+ if (collectionItem.request) {
+ if (postwomanCollection[0]) {
+ postwomanCollection[0].name = collection.info ? collection.info.name : "";
+ postwomanCollection[0].requests.push(this.parsePostmanRequest(collectionItem));
+ } else {
+ postwomanCollection.name = collection.name ? collection.name
+ : "";
+ postwomanCollection.requests.push(this.parsePostmanRequest(collectionItem));
+ }
+ } else if (collectionItem.item) {
+ if (collectionItem.item[0]) {
+ postwomanCollection[0].folders.push(this.parsePostmanCollection(collectionItem, false));
+ }
+ }
+ }
+ return postwomanCollection;
+ },
+ parsePostmanRequest(requestObject) {
+ let pwRequest = {
+ "url": "",
+ "path": "",
+ "method": "",
+ "auth": "",
+ "httpUser": "",
+ "httpPassword": "",
+ "passwordFieldType": "password",
+ "bearerToken": "",
+ "headers": [],
+ "params": [],
+ "bodyParams": [],
+ "rawParams": "",
+ "rawInput": false,
+ "contentType": "",
+ "requestType": "",
+ "name": "",
+ };
+
+ pwRequest.name = requestObject.name;
+ let requestObjectUrl = requestObject.request.url.raw.match(/^(.+:\/\/[^\/]+|{[^\/]+})(\/[^\?]+|).*$/);
+ pwRequest.url = requestObjectUrl[1];
+ pwRequest.path = requestObjectUrl[2] ? requestObjectUrl[2] : "";
+ pwRequest.method = requestObject.request.method;
+ let itemAuth = requestObject.request.auth ? requestObject.request.auth
+ : "";
+ let authType = itemAuth ? itemAuth.type
+ : "";
+ if (authType === "basic") {
+ pwRequest.auth = "Basic Auth";
+ pwRequest.httpUser = itemAuth.basic[0].key === "username"
+ ? itemAuth.basic[0].value
+ : itemAuth.basic[1].value;
+ pwRequest.httpPassword = itemAuth.basic[0].key === "password"
+ ? itemAuth.basic[0].value
+ : itemAuth.basic[1].value;
+ } else if (authType === "oauth2") {
+ pwRequest.auth = "OAuth 2.0";
+ pwRequest.bearerToken = itemAuth.oauth2[0].key === "accessToken"
+ ? itemAuth.oauth2[0].value
+ : itemAuth.oauth2[1].value;
+ } else if (authType === "bearer") {
+ pwRequest.auth = "Bearer Token";
+ pwRequest.bearerToken = itemAuth.bearer[0].value;
+ }
+ let requestObjectHeaders = requestObject.request.header;
+ if (requestObjectHeaders) {
+ pwRequest.headers = requestObjectHeaders;
+ for (let header of pwRequest.headers) {
+ delete header.name;
+ delete header.type;
+ }
+ }
+ let requestObjectParams = requestObject.request.url.query;
+ if (requestObjectParams) {
+ pwRequest.params = requestObjectParams;
+ for (let param of pwRequest.params) {
+ delete param.disabled;
+ }
+ }
+ if (requestObject.request.body) {
+ if (requestObject.request.body.mode === "urlencoded") {
+ let params = requestObject.request.body.urlencoded;
+ pwRequest.bodyParams = params ? params : [];
+ for(let param of pwRequest.bodyParams) {
+ delete param.type;
+ }
+ } else if (requestObject.request.body.mode === "raw") {
+ pwRequest.rawInput = true;
+ pwRequest.rawParams = requestObject.request.body.raw;
+ }
+ }
+ return pwRequest;
}
}
};
diff --git a/lang/en-US.js b/lang/en-US.js
index 9191cec5f..63b786060 100644
--- a/lang/en-US.js
+++ b/lang/en-US.js
@@ -222,6 +222,7 @@ export default {
payload: "Payload",
choose_file: "Choose a file",
file_imported: "File imported",
+ import_failed: "Import failed",
f12_details: "(F12 for details)",
we_use_cookies: "We use cookies",
copied_to_clipboard: "Copied to clipboard",