0

I am trying to upload a text file, to attach to an unpublished Knowledge article in the Utah release of ServiceNow.

My code is:

$uri = "https://instanceName.service-now.com/api/now/attachment/upload"
$method = "POST"
$filePath = "C:\it\test.txt"
$headers = @{
    "Accept" = "application/json"
}
$uploadResult = Invoke-RestMethod -Uri $uri -Method Post -Headers $headers -Credential $credential -ContentType "multipart/form-data" -Form @{
    "table_name"   = "kb_knowledge"
    "table_sys_id" = "fcd86f8fc314b5500240d1ec7a01319f" # Sys_id of the article, to which the file should be attached.
    "uploadFile"   = Get-Item -Path $filePath
}

It looks to me, like the account represented in $credential can add attachments (it can definitely create KM articles).

The error:

Invoke-RestMethod: {"error":{"message":"Missing parameter: table_name","detail":null},"status":"failure"}

I tried wrapping the form value fields in double quotes (and single quotes, same behavior):

$uploadResult = Invoke-RestMethod -Uri $uri -Method Post -Headers $headers -Credential $credential -ContentType "multipart/form-data" -Form @{
    table_name   = "`"kb_knowledge`""
    table_sys_id = "`"fcd86f8fc314b5500240d1ec7a01319f`"" # Sys_id of the article, to which the file should be attached.
    uploadFile   = "`"$(Get-Item -Path $filePath)`""
}

But that only resulted in a different error:

Invoke-RestMethod: {"error":{"message":"Failed to create the attachment. File part might be missing in the request.","detail":null},"status":"failure"}

I am certain that a file exists at C:\it\test.txt. Also, I am using /upload instead of /file (https://developer.servicenow.com/dev.do#!/reference/api/utah/rest/c_AttachmentAPI#attachment-POST-upload) because I want to avoid having to figure out the mime type of the file.

What gives?

StackExchangeGuy
  • 741
  • 16
  • 36
  • the message is MIME : "multipart/form-data". Mime expects attachment to start with two dashes on new line. See : https://learn.microsoft.com/en-us/previous-versions/office/developer/exchange-server-2010/aa563375(v=exchg.140) – jdweng Aug 01 '23 at 20:44
  • I do not understand what you are suggesting. The cURL example from the developer doc shows that the content type should be "multipart/mform-data". – StackExchangeGuy Aug 01 '23 at 22:17
  • You are not running CURL. CURL is using methods to create a proper MIME text. MIME is the text in the body of an HTTP request/response. The MIME sample shows the text. The CURL code running CURL methods that create the MIME text. Don't use the CURL example if you are not running CURL. Use Powershell examples on this page : https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-restmethod?view=powershell-7.3#example-4-simplified-multipart-form-data-submission – jdweng Aug 02 '23 at 06:13
  • That example sure does look /a lot/ like my code. – StackExchangeGuy Aug 02 '23 at 22:30
  • It does look similar except for the fact of the escape characters around the double quotes, there is no Accept method, and it is not multi-part. There are a few different examples that can be used. You do not need multi-part. – jdweng Aug 02 '23 at 23:46

0 Answers0