The Now Platform® Washington DC release is live. Watch now!

Help
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

POSTing file to sys_import.do with Powershell

NT35
Kilo Explorer

We are trying to update a table using /sys_import.do?sysparm_import_set_tablename=u_kronos_department_import. I have the below Powershell, and when run I do get an HTTP 200 response, but the file does not appear to load. I can run an identical upload in POSTMAN and it is successful.

find_real_file.png

# Eg. User name="admin", Password="admin" for this code sample.
$user = "usr"
$pass = "pwd"
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

# Build auth header
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user, $pass)))

# Set proper headers
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add('Authorization',('Basic {0}' -f $base64AuthInfo))
$headers.Add('Accept','application/json')
#$headers.Add('Content-Type','application/json')
$headers.Add('Content-Type','multipart/form-data')

# Specify endpoint uri
$uri = "https://DEV.service-now.com/sys_import.do?sysparm_import_set_tablename=u_kronos_department_import&sysparm_transform_after_load=true"


# Specify HTTP method (POST, PATCH, PUT)
$method = "POST"

#Standard method?
<# 
$body = @{
    "file"=(Get-Item -Path "Departments.csv")
}
$bodyJson = $body |convertto-json
# Send HTTP request
$response = Invoke-WebRequest -Headers $headers -Method $method -Uri $uri -Body $bodyJson -ContentType "application/json"
 #>

#Infile Method, https://community.servicenow.com/community?id=community_question&sys_id=d3707023dbaceb8023f4a345ca961949&view_source=searchResult
$fileToAttach =  "Departments.csv"
$response = Invoke-WebRequest -Headers $headers -Method $method -Uri $uri -InFile $fileToAttach


# Print response
$response

 

8 REPLIES 8

The Machine
Kilo Sage

I think the first problem is this part.

$headers.Add('Content-Type','multipart/form-data')

ServiceNow supports this but to my knowledge on a limited case and requires Integration Hub. 

Since it's a CSV file, could you store it on an SFTP server and pull it via data source?

What's odd is I'm able to use multipart/form-data in the Postman request without problems.

Yeah Postman supports it with no problem.  ServiceNow "kinda" does.  There's definitely an easier way of doing this, but unsure if you have other restrictions or limitations that are preventing you from using those methods. 

We have an outside consultant doing the Service Now configuration so I'm not too familiar with what can be done on that end. I am working with them to move some custom work order forms we had in Sharepoint into Service Now, but one of the requirements is a nightly feed of departments. We were sent a functioning Postman request to convert to Powershell or other method.