0
votes

I tried many ways of uploading a CSV file to ServiceNow using PowerShell via Invoke-RestMethod and Invoke-WebRequest; however, I have hit a wall. When I call the functions, I receive the following error:

"Invoke-RestMethod : The remote server returned an error: (405) Method Not Allowed."

"Invoke-WebRequest : The remote server returned an error: (405) Method Not Allowed."

I have tried the code below:

Attempt 1)

$Headers = @{'Auth_token'=$envCred};
$FileContent = [IO.File]::ReadAllText('C:\temp\test.csv');
$Fields = @{'uploadFile'=$FileContent};

Invoke-WebRequest -Uri $Uri -ContentType 'multipart/form-data' -Method Post -Headers $Headers -Body $Fields;

Attempt 2)

Invoke-RestMethod -Method Post -Uri $uri -Credential $envCred -ContentType 'multipart/form-data -InFile "C:\temp\test.csv" ' 

I know for a fact that the API is working, because after I call: Invoke-RestMethod -Method 'get' -Uri $uri -Credential $snCred -body $body it returns the proper information.

I also tried the [-Method Patch] with the following: "Invoke-RestMethod -Uri $uri -Credential $snCred -Method Patch -ContentType 'text/csv' -InFile "C:\temp\test.csv" - Also tried with the -ContentType 'multipart/form-data' - I get the following error: "Invoke-RestMethod : The remote server returned an error: (415) Unsupported Media Type."

Is there another way or uploading CSV file(s) in PowerShell using the Invoke-RestMethod or Invoke-WebRequest?

Solution: I realized that I had a typo with my URI, after fixing it, it worked! Sorry for the inconvenience.

1
What's the URI you're trying to hit in ServiceNow? Please provide the endpoint :)FoxDeploy
@FoxDeploy: the URI that i'm targeting in ServiceNow is a specific Task record. Should I point my URI to the attachment/Upload URI? If I do that, how would I specify the task to upload the CSV to?Engah

1 Answers

0
votes

It's unclear what endpoint you were using, however, here's an example from the docs

curl "https://instance.service-now.com/api/now/attachment/upload" \
--request POST \
--header "Accept:application/json"
--user 'admin':'admin'"\
--header "Content-Type:multipart/form-data"
-F 'table_name=incident' -F 'table_sys_id=d71f7935c0a8016700802b64c67c11c6' -F 'encryption_context=undefined'-F 'uploadFile=@ location of the file on file system'