SP-Config Import Fails with 400 Bad Request Message

I am trying to take advantage of the SP-Config API that is currently in beta to export and import Source objects using PowerShell. I have the export side working just fine. The import side is not working. IdentityNow consistently returns 400 Bad Request when I try to start an import job. How do I need to format the contents of the request? Are there particular difficulties with PowerShell here? Why is the code below not working? What are good strategies for debugging it?

The Source file that I am using as a guinea pig was created as a result of using SP-Config to export its definition from a first tenant. I am trying to import it for the first time into a second tenant. The PowerShell code below captures the essence of what is happening for this test case.

# already verified preconditions:
#   the routine to retrieve an $accessToken works and supplies a valid token consistently
#   $tenant and $importFileName have valid values
#   file referenced by $importFileName contains valid JSON for a Source object

[Hashtable]$bulkFileContents = @{
  version = 1
  timestamp = Get-Date -Format "o"
  options = @{
    includeTypes = @("SOURCE")
  }
  objects = @(
    @{
      version = 1
      self = @{
        type = "SOURCE"
        name = "AD - Contoso"
      }
      object = Get-Content -Path $importFileName -Raw | ConvertFrom-Json -AsHashtable
    }
  )
}
[FileInfo]$tempFile = New-TemporaryFile
Set-Content -Path $tempFile -Value ($bulkFileContents | ConvertTo-Json -Depth 100)
[Hashtable]$formContents = @{
  data = $tempFile
  options = @{
    includeTypes = @("SOURCE")
    objectOptions = @{
      "SOURCE" = @{
        includedNames = @("AD - Contoso")
      }
    }
  }
}
[Hashtable]$requestParameters = @{
  Method = [WebRequestMethod]::Post
  Uri = "https://$tenant.api.identitynow.com/beta/sp-config/import"
  Authentication = [WebAuthenticationType]::Bearer
  Token = $accessToken
  Form = $formContents
}
[BasicHtmlWebResponseObject]$htmlResponse = Invoke-WebRequest @requestParameters

The response from IdentityNow is a simple HTML 400, with no further diagnostic information about why the request was bad. I have tried using the preview switch, but it had no effect.

Another user had a similar issue when using Powershell to upload a file to one of our endpoints. Powershell can be tricky, so please take a close look at how the solution to NELM Bulk Update API forms the Powershell request. Note, the post I shared is working with CSV data, whereas SP-Config is JSON. You will want to make sure your request body specifies a content type of application/json.

Colin,

Thanks for the pointers. The code listing in the other article is a bit dated. For instance, the UseBasicParsing parameter is now deprecated.

Here is a code listing that I got to work:

# already verified preconditions:
#   the routine to retrieve an $accessToken works and supplies a valid token consistently
#   $tenant and $importFileName have valid values
#   file referenced by $importFileName contains valid JSON for a Source object

[Hashtable]$bulkFileContents = @{
  tenant = $tenant
  version = 1
  timestamp = Get-Date -Format "o"
  options = @{
    includeTypes = @("SOURCE")
    objectOptions = $null
  }
  objects = @(
    @{
      version = 1
      self = @{
        type = "SOURCE"
        name = "AD - Contoso"
      }
      object = Get-Content -Path $importFileName -Raw | ConvertFrom-Json -AsHashtable
    }
  )
}
[FileInfo]$tempFile = New-TemporaryFile
Set-Content -Path $tempFile -Value ($bulkFileContents | ConvertTo-Json -Depth 100)
[Hashtable]$formContents = @{
  data = $tempFile
}
[Hashtable]$requestParameters = @{
  Method = [WebRequestMethod]::Post
  Uri = "https://$tenant.api.identitynow.com/beta/sp-config/import"
  Authentication = [WebAuthenticationType]::Bearer
  Token = $accessToken
  Form = $formContents
}
[BasicHtmlWebResponseObject]$htmlResponse = Invoke-WebRequest @requestParameters
2 Likes