Upload CSV file with the API
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2023 06:36 PM
Hello,
I am struggling with how to upload a CSV file to a data source record where it will then be consumed and update a table. The file contains only 1 column with a header and I am calling the API from powershell:
$user = <<USER_NAME>>
$pass = <<USER_PASSWORD>>
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user, $pass)))
$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')
$uri = <<SERVICENOW_URL>> + "api/now/attachment/file?table_name=sys_data_source&table_sys_id=<<RECORD_SYS_ID>>&file_name=Infor.csv"
$fileToAttach = <<PATH_TO_FILE>>
$method = "POST"
$response = Invoke-WebRequest -Headers $headers -Method $method -Uri $uri -InFile $fileToAttach
The file successfully gets uploaded to the data source record and when I click on it, I can see the file in a popup window and it looks fine. However when I click the related link 'Test Load 20 Records' to create the columns in the import set table, the column that gets created isn't correct and the test load doesn't actually load anything to the import set. See attached picture of the column that gets created in the import set table. The header in the CSV file is just JobTitle.
Am I calling the API wrong? Does the file encoding need to be something specific in order for ServiceNow to properly recognize the data so the table columns can be created for the first time?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2023 06:44 PM
Hi,
I'd recommend checking out this similar thread for an example PowerShell script you can reference: https://www.servicenow.com/community/developer-forum/file-import-csv-from-mid-server-windows-2019/m-... - check content type.
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!