Chilkat Online Tools

C / Postman API / Import exported data

Back to Collection Items

#include <C_CkHttp.h>
#include <C_CkHttpRequest.h>
#include <C_CkHttpResponse.h>
#include <C_CkStringBuilder.h>
#include <C_CkJsonObject.h>

void ChilkatSample(void)
    {
    HCkHttp http;
    BOOL success;
    HCkHttpRequest req;
    HCkHttpResponse resp;
    HCkStringBuilder sbResponseBody;
    HCkJsonObject jResp;
    int respStatusCode;
    const char *id;
    const char *name;
    const char *uid;
    int i;
    int count_i;

    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    http = CkHttp_Create();

    req = CkHttpRequest_Create();
    CkHttpRequest_putHttpVerb(req,"POST");
    CkHttpRequest_putPath(req,"/import/exported");
    CkHttpRequest_putContentType(req,"multipart/form-data");
    CkHttpRequest_AddParam(req,"","");

    success = CkHttpRequest_AddFileForUpload2(req,"file"," path to file","application/octet-stream");

    CkHttpRequest_AddHeader(req,"Expect","100-continue");
    CkHttpRequest_AddHeader(req,"X-API-Key","{{postman_api_key}}");

    resp = CkHttp_SynchronousRequest(http,"api.getpostman.com",443,TRUE,req);
    if (CkHttp_getLastMethodSuccess(http) == FALSE) {
        printf("%s\n",CkHttp_lastErrorText(http));
        CkHttp_Dispose(http);
        CkHttpRequest_Dispose(req);
        return;
    }

    sbResponseBody = CkStringBuilder_Create();
    CkHttpResponse_GetBodySb(resp,sbResponseBody);

    jResp = CkJsonObject_Create();
    CkJsonObject_LoadSb(jResp,sbResponseBody);
    CkJsonObject_putEmitCompact(jResp,FALSE);

    printf("Response Body:\n");
    printf("%s\n",CkJsonObject_emit(jResp));

    respStatusCode = CkHttpResponse_getStatusCode(resp);
    printf("Response Status Code = %d\n",respStatusCode);
    if (respStatusCode >= 400) {
        printf("Response Header:\n");
        printf("%s\n",CkHttpResponse_header(resp));
        printf("Failed.\n");
        CkHttpResponse_Dispose(resp);
        CkHttp_Dispose(http);
        CkHttpRequest_Dispose(req);
        CkStringBuilder_Dispose(sbResponseBody);
        CkJsonObject_Dispose(jResp);
        return;
    }

    CkHttpResponse_Dispose(resp);

    // Sample JSON response:
    // (Sample code for parsing the JSON response is shown below)

    // {
    //   "collections": [
    //     {
    //       "id": "e956ff02-7597-4f8d-a5e7-1c18208c9e56",
    //       "name": "Swagger Petstore",
    //       "uid": "2282-e956ff02-7597-4f8d-a5e7-1c18208c9e56"
    //     }
    //   ]
    // }

    // Sample code for parsing the JSON response...
    // Use this online tool to generate parsing code from sample JSON: Generate JSON Parsing Code

    // Chilkat functions returning "const char *" return a pointer to temporary internal memory owned and managed by Chilkat.

    i = 0;
    count_i = CkJsonObject_SizeOfArray(jResp,"collections");
    while (i < count_i) {
        CkJsonObject_putI(jResp,i);
        id = CkJsonObject_stringOf(jResp,"collections[i].id");
        name = CkJsonObject_stringOf(jResp,"collections[i].name");
        uid = CkJsonObject_stringOf(jResp,"collections[i].uid");
        i = i + 1;
    }



    CkHttp_Dispose(http);
    CkHttpRequest_Dispose(req);
    CkStringBuilder_Dispose(sbResponseBody);
    CkJsonObject_Dispose(jResp);

    }

Curl Command

curl -X POST
	-H "X-API-Key: {{postman_api_key}}"
	-H "Content-Type: multipart/form-data"
	--form 'type=file'
	--form 'file=@"/path/to/file"'
https://api.getpostman.com/import/exported

Postman Collection Item JSON

{
  "name": "Import exported data",
  "request": {
    "method": "POST",
    "header": [
      {
        "name": "Content-Type",
        "key": "Content-Type",
        "value": "multipart/form-data"
      }
    ],
    "body": {
      "mode": "formdata",
      "formdata": [
        {
          "key": "type",
          "value": "file",
          "type": "text"
        },
        {
          "key": "file",
          "description": "File containing the exported collections or environments",
          "type": "file",
          "src": [
          ]
        }
      ]
    },
    "url": {
      "raw": "https://api.getpostman.com/import/exported",
      "protocol": "https",
      "host": [
        "api",
        "getpostman",
        "com"
      ],
      "path": [
        "import",
        "exported"
      ]
    },
    "description": "This endpoint allows you to import your exported Postman data.\nFor more information about how you can export your data, refer <a href=\"https://go.postman.co/me/export\">Export your Postman data.</a>\n\nOn successful imports, the response will be an array with Each element contaning `id`, `name` and `uid` of entities created.\n\n**Note**: Refer to examples for different scenarios.\n\n> Requires <a href=\"#authentication\">API Key</a> as `X-Api-Key` request header."
  },
  "response": [
    {
      "name": "Success: Import to a specific workspace with workspace ID passed in the headers",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "X-Workspace-Id",
            "value": "c90a3482-f80d-43d6-9dfb-0830f5e43605"
          },
          {
            "name": "Content-Type",
            "key": "Content-Type",
            "value": "application/json"
          }
        ],
        "body": {
          "mode": "formdata",
          "formdata": [
            {
              "key": "type",
              "value": "file",
              "type": "text"
            },
            {
              "key": "file",
              "value": "",
              "type": "file"
            }
          ]
        },
        "url": {
          "raw": "https://api.getpostman.com/import/exported",
          "protocol": "https",
          "host": [
            "api",
            "getpostman",
            "com"
          ],
          "path": [
            "import",
            "exported"
          ],
          "variable": [
            {
              "key": "import_type",
              "value": "openapi"
            }
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json",
          "description": {
            "content": "",
            "type": "text/plain"
          }
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"collections\": [\n        {\n            \"id\": \"e956ff02-7597-4f8d-a5e7-1c18208c9e56\",\n            \"name\": \"Swagger Petstore\",\n            \"uid\": \"2282-e956ff02-7597-4f8d-a5e7-1c18208c9e56\"\n        }\n    ]\n}"
    },
    {
      "name": "Error: Invalid Params",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "name": "Content-Type",
            "key": "Content-Type",
            "value": "application/json"
          }
        ],
        "body": {
          "mode": "formdata",
          "formdata": [
            {
              "key": "type",
              "value": "invalid_type",
              "type": "text"
            },
            {
              "key": "input",
              "value": "",
              "type": "file"
            }
          ]
        },
        "url": {
          "raw": "https://api.getpostman.com/import/exported",
          "protocol": "https",
          "host": [
            "api",
            "getpostman",
            "com"
          ],
          "path": [
            "import",
            "exported"
          ],
          "variable": [
            {
              "key": "import_type",
              "value": "openapi"
            }
          ]
        }
      },
      "status": "Bad Request",
      "code": 400,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json",
          "description": {
            "content": "",
            "type": "text/plain"
          }
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"error\": {\n        \"name\": \"invalidParamsError\",\n        \"message\": \"The request body has invalid values for the type parameter. Value must be one of file, string, json\",\n        \"details\": {\n            \"param\": \"type\"\n        }\n    }\n}"
    },
    {
      "name": "Success: Import the exported file",
      "originalRequest": {
        "method": "POST",
        "header": [
        ],
        "body": {
          "mode": "formdata",
          "formdata": [
            {
              "key": "type",
              "value": "file",
              "type": "text"
            },
            {
              "key": "input",
              "value": "",
              "description": "File containing your exported collections and environments",
              "type": "file"
            }
          ]
        },
        "url": {
          "raw": "https://api.getpostman.com/import/exported",
          "protocol": "https",
          "host": [
            "api",
            "getpostman",
            "com"
          ],
          "path": [
            "import",
            "exported"
          ],
          "variable": [
            {
              "key": "import_type",
              "value": ""
            }
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json",
          "description": {
            "content": "",
            "type": "text/plain"
          }
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"collections\": [\n        {\n            \"id\": \"c74f2169-0c59-49f6-b04f-fdec4aba3db3\",\n            \"name\": \"demo\",\n            \"uid\": \"2282-c74f2169-0c59-49f6-b04f-fdec4aba3db3\"\n        },\n        {\n            \"id\": \"49b08197-6b3d-4957-88d3-dbba0e3ee6a0\",\n            \"name\": \"demo\",\n            \"uid\": \"2282-49b08197-6b3d-4957-88d3-dbba0e3ee6a0\"\n        }\n    ]\n}"
    },
    {
      "name": "Error: Param Missing",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "name": "Content-Type",
            "key": "Content-Type",
            "value": "application/json"
          }
        ],
        "body": {
          "mode": "formdata",
          "formdata": [
            {
              "key": "input",
              "value": "",
              "type": "file"
            }
          ]
        },
        "url": {
          "raw": "https://api.getpostman.com/import/exported",
          "protocol": "https",
          "host": [
            "api",
            "getpostman",
            "com"
          ],
          "path": [
            "import",
            "exported"
          ],
          "variable": [
            {
              "key": "import_type",
              "value": "openapi"
            }
          ]
        }
      },
      "status": "Bad Request",
      "code": 400,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json",
          "description": {
            "content": "",
            "type": "text/plain"
          }
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"error\": {\n        \"name\": \"paramMissingError\",\n        \"message\": \"The request body is missing a value for the type parameter. Check your request and try again.\",\n        \"details\": {\n            \"param\": \"type\"\n        }\n    }\n}"
    },
    {
      "name": "Success: Import to a specific workspace with workspace ID passed as a query parameter",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "name": "Content-Type",
            "key": "Content-Type",
            "value": "application/json"
          }
        ],
        "body": {
          "mode": "formdata",
          "formdata": [
            {
              "key": "type",
              "value": "file",
              "type": "text"
            },
            {
              "key": "input",
              "value": "",
              "type": "file"
            }
          ]
        },
        "url": {
          "raw": "https://api.getpostman.com/import/exported?workspace=c90a3482-f80d-43d6-9dfb-0830f5e43605",
          "protocol": "https",
          "host": [
            "api",
            "getpostman",
            "com"
          ],
          "path": [
            "import",
            "exported"
          ],
          "query": [
            {
              "key": "workspace",
              "value": "c90a3482-f80d-43d6-9dfb-0830f5e43605"
            }
          ],
          "variable": [
            {
              "key": "import_type",
              "value": "openapi"
            }
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json",
          "description": {
            "content": "",
            "type": "text/plain"
          }
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"collections\": [\n        {\n            \"id\": \"b31be584-1b1e-4444-b581-761edf88fe77\",\n            \"name\": \"Swagger Petstore\",\n            \"uid\": \"2282-b31be584-1b1e-4444-b581-761edf88fe77\"\n        }\n    ]\n}"
    }
  ]
}