PingAuthorize API Reference

Import Snapshot

POST {{apiPath}}/snapshot/{{branchName}}/import

The POST /snapshot/{{branchName}}/import operation imports a snapshot as a new policy branch. The operation uses the branch name specified in the request URL for the newly created policy branch. If you have existing self-governance policies in a policy snapshot, you can import them using the standard snapshot import process, with some minor adjustments. For more information, refer to Importing a self-governance policy snapshot in the PingAuthorize Policy Administration Guide.

Prerequisites

Request Model

For property descriptions, refer to Snapshot service data model.

Property Type Required

<objectName>

JSON[]

Required

Create a new snapshot to commit the initial state of the policy branch.

Headers

Content-Type      multipart/form-data; boundary=<calculated when request is sent>

x-user-id      {{userId}}

Body

formdata

Key Type Value

file

Example Request

  • cURL

  • C#

  • Go

  • HTTP

  • Java

  • jQuery

  • NodeJS

  • Python

  • PHP

  • Ruby

  • Swift

curl --location --globoff '{{apiPath}}/snapshot/{{branchName}}/import' \
--header 'x-user-id: {{userId}}' \
--form '=@"1CRHSZRvl/demo-branch_new-commit.snapshot"'
var options = new RestClientOptions("{{apiPath}}/snapshot/{{branchName}}/import")
{
  MaxTimeout = -1,
};
var client = new RestClient(options);
var request = new RestRequest("", Method.Post);
request.AddHeader("x-user-id", "{{userId}}");
request.AlwaysMultipartFormData = true;
request.AddFile("", "1CRHSZRvl/demo-branch_new-commit.snapshot");
RestResponse response = await client.ExecuteAsync(request);
Console.WriteLine(response.Content);
package main

import (
  "fmt"
  "bytes"
  "mime/multipart"
  "os"
  "path/filepath"
  "net/http"
  "io"
)

func main() {

  url := "{{apiPath}}/snapshot/{{branchName}}/import"
  method := "POST"

  payload := &bytes.Buffer{}
  writer := multipart.NewWriter(payload)
  file, errFile1 := os.Open("1CRHSZRvl/demo-branch_new-commit.snapshot")
  defer file.Close()
  part1,
         errFile1 := writer.CreateFormFile("",filepath.Base("1CRHSZRvl/demo-branch_new-commit.snapshot"))
  _, errFile1 = io.Copy(part1, file)
  if errFile1 != nil {
    fmt.Println(errFile1)
    return
  }
  err := writer.Close()
  if err != nil {
    fmt.Println(err)
    return
  }


  client := &http.Client {
  }
  req, err := http.NewRequest(method, url, payload)

  if err != nil {
    fmt.Println(err)
    return
  }
  req.Header.Add("x-user-id", "{{userId}}")

  req.Header.Set("Content-Type", writer.FormDataContentType())
  res, err := client.Do(req)
  if err != nil {
    fmt.Println(err)
    return
  }
  defer res.Body.Close()

  body, err := io.ReadAll(res.Body)
  if err != nil {
    fmt.Println(err)
    return
  }
  fmt.Println(string(body))
}
POST /snapshot/{{branchName}}/import HTTP/1.1
Host: {{apiPath}}
x-user-id: {{userId}}
Content-Length: 214
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW

------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name=""; filename="demo-branch_new-commit.snapshot"
Content-Type: <Content-Type header here>

(data)
------WebKitFormBoundary7MA4YWxkTrZu0gW--
OkHttpClient client = new OkHttpClient().newBuilder()
  .build();
MediaType mediaType = MediaType.parse("text/plain");
RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
  .addFormDataPart("","demo-branch_new-commit.snapshot",
    RequestBody.create(MediaType.parse("application/octet-stream"),
    new File("1CRHSZRvl/demo-branch_new-commit.snapshot")))
  .build();
Request request = new Request.Builder()
  .url("{{apiPath}}/snapshot/{{branchName}}/import")
  .method("POST", body)
  .addHeader("x-user-id", "{{userId}}")
  .addHeader("Content-Length", "")
  .build();
Response response = client.newCall(request).execute();
var form = new FormData();
form.append("", fileInput.files[0], "demo-branch_new-commit.snapshot");

var settings = {
  "url": "{{apiPath}}/snapshot/{{branchName}}/import",
  "method": "POST",
  "timeout": 0,
  "headers": {
    "x-user-id": "{{userId}}",
    "Content-Length": ""
  },
  "processData": false,
  "mimeType": "multipart/form-data",
  "contentType": false,
  "data": form
};

$.ajax(settings).done(function (response) {
  console.log(response);
});
var request = require('request');
var fs = require('fs');
var options = {
  'method': 'POST',
  'url': '{{apiPath}}/snapshot/{{branchName}}/import',
  'headers': {
    'x-user-id': '{{userId}}',
    'Content-Length': ''
  },
  formData: {
    '': {
      'value': fs.createReadStream('1CRHSZRvl/demo-branch_new-commit.snapshot'),
      'options': {
        'filename': 'demo-branch_new-commit.snapshot',
        'contentType': null
      }
    }
  }
};
request(options, function (error, response) {
  if (error) throw new Error(error);
  console.log(response.body);
});
import requests

url = "{{apiPath}}/snapshot/{{branchName}}/import"

payload = {}
files=[
  ('',('demo-branch_new-commit.snapshot',open('1CRHSZRvl/demo-branch_new-commit.snapshot','rb'),'application/octet-stream'))
]
headers = {
  'x-user-id': '{{userId}}',
  'Content-Length': ''
}

response = requests.request("POST", url, headers=headers, data=payload, files=files)

print(response.text)
<?php
require_once 'HTTP/Request2.php';
$request = new HTTP_Request2();
$request->setUrl('{{apiPath}}/snapshot/{{branchName}}/import');
$request->setMethod(HTTP_Request2::METHOD_POST);
$request->setConfig(array(
  'follow_redirects' => TRUE
));
$request->setHeader(array(
  'x-user-id' => '{{userId}}',
  'Content-Length' => ''
));
$request->addUpload('', '1CRHSZRvl/demo-branch_new-commit.snapshot', 'demo-branch_new-commit.snapshot', '<Content-Type Header>');
try {
  $response = $request->send();
  if ($response->getStatus() == 200) {
    echo $response->getBody();
  }
  else {
    echo 'Unexpected HTTP status: ' . $response->getStatus() . ' ' .
    $response->getReasonPhrase();
  }
}
catch(HTTP_Request2_Exception $e) {
  echo 'Error: ' . $e->getMessage();
}
require "uri"
require "net/http"

url = URI("{{apiPath}}/snapshot/{{branchName}}/import")

http = Net::HTTP.new(url.host, url.port);
request = Net::HTTP::Post.new(url)
request["x-user-id"] = "{{userId}}"
request["Content-Length"] = ""
form_data = [['', File.open('1CRHSZRvl/demo-branch_new-commit.snapshot')]]
request.set_form form_data, 'multipart/form-data'
response = http.request(request)
puts response.read_body
let parameters = [
  [
    "key": "",
    "src": "1CRHSZRvl/demo-branch_new-commit.snapshot",
    "type": "file"
  ]] as [[String: Any]]

let boundary = "Boundary-\(UUID().uuidString)"
var body = Data()
var error: Error? = nil
for param in parameters {
  if param["disabled"] != nil { continue }
  let paramName = param["key"]!
  body += Data("--\(boundary)\r\n".utf8)
  body += Data("Content-Disposition:form-data; name=\"\(paramName)\"".utf8)
  if param["contentType"] != nil {
    body += Data("\r\nContent-Type: \(param["contentType"] as! String)".utf8)
  }
  let paramType = param["type"] as! String
  if paramType == "text" {
    let paramValue = param["value"] as! String
    body += Data("\r\n\r\n\(paramValue)\r\n".utf8)
  } else {
    let paramSrc = param["src"] as! String
    let fileURL = URL(fileURLWithPath: paramSrc)
    if let fileContent = try? Data(contentsOf: fileURL) {
      body += Data("; filename=\"\(paramSrc)\"\r\n".utf8)
      body += Data("Content-Type: \"content-type header\"\r\n".utf8)
      body += Data("\r\n".utf8)
      body += fileContent
      body += Data("\r\n".utf8)
    }
  }
}
body += Data("--\(boundary)--\r\n".utf8);
let postData = body


var request = URLRequest(url: URL(string: "{{apiPath}}/snapshot/{{branchName}}/import")!,timeoutInterval: Double.infinity)
request.addValue("{{userId}}", forHTTPHeaderField: "x-user-id")
request.addValue("", forHTTPHeaderField: "Content-Length")
request.addValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")

request.httpMethod = "POST"
request.httpBody = postData

let task = URLSession.shared.dataTask(with: request) { data, response, error in
  guard let data = data else {
    print(String(describing: error))
    return
  }
  print(String(data: data, encoding: .utf8)!)
}

task.resume()

Example Response

200 OK

{
    "id": "0533180c-f7ff-4f4b-a6ed-fc6956a7c91e",
    "name": "example-import",
    "head": "bb23e640-86e0-4373-ab95-64aaf86b4e9a",
    "tip": {
        "id": "967c030b-a376-4691-9abe-3d210ed4ab2b",
        "parentId": "bb23e640-86e0-4373-ab95-64aaf86b4e9a",
        "state": "UNCOMMITTED",
        "commitDetails": {
            "userId": null,
            "dateTime": null,
            "message": null
        },
        "approvals": [],
        "branchId": "0533180c-f7ff-4f4b-a6ed-fc6956a7c91e"
    },
    "parentId": "62c53d22-f1e1-4507-a208-a983ec5f3e12"
}