Powershell to update tags using API

I'm using Invoke-Webrequest to emulate the curl command in the API for updating tags using a file. I've used community posts to resolve most of my issues. I can successfully connect with the API and post the JSON portion. However, I've not been able to upload the file that contains the tags. The curl example shows this as '[email protected]' after the JSON portion of the post. I've tried Invoke-RestMethod as well. No matter how I format it, I can't seem to emulate that portion of the curl and the response is always "Tag file is missing". Any help would be appreciated.
Answers
further examined the example by DMuncy and encoded the filename to get past the "Tag file is missing"
Now I get "Could not split the file on multiple lines. The file probably does not have proper End Of Line characters"
Confirmed csv has CR LF end of each line.
Following is the current code
$ptruserID =
$ptrusersec =
$PolicyID =
$dependency = $false
$glCodes =$true
$header = $true
$setRequired=$false
$json = [ordered]@{
"type"="update";
"credentials"[email protected]{
"partnerUserID"=$ptruserID;
"partnerUserSecret"=$ptrusersec
};
"inputSettings"[email protected]{
"type"="policy";
"policyIDList"[email protected]($PolicyID)
};
"tags"[email protected]{
"action"="replace";
"source"="file";
"config"[email protected]{
"dependency"=$dependency;
"glCodes"=$glCodes;
"header"=$header;
"setRequired"=$setRequired;
"fileType"="csv"
};
};
} | ConvertTo-Json
$url = "https://integrations.expensify.com/Integration-Server/ExpensifyIntegrations"
$request = "requestJobDescription="
$file = Get-Item -Path C:\PM_to_Expensify.csv
$encode = [System.Web.HttpUtility]::UrlEncode($file)
$body = $request + $json + "'&file='$encode"
Invoke-Webrequest -uri $url -body $body -Method Post
- Spam
- Abuse
- Report
0 · Accept Answer Off Topic Insightful Vote Up AwesomeAny chance you can check the value of
[System.Web.HttpUtility]::UrlEncode($file)
and see if the line breaks are preserved? The result shouldn't be on one single line.If the line breaks are still present as expected, can you try to convert them to just
LF
(instead of CRLF)?Cheers
- Spam
- Abuse
- Report
1 · Accept Answer Off Topic 1Insightful Vote Up Awesome- full path to the file
- relative path to the file
- encoded paths (using [System.Web.HttpUtility]::UrlEncode)
- content of the file using Get-Content
- encoded content of the file
For the file itself, I've tried:- Spam
- Abuse
- Report
0 · Accept Answer Off Topic Insightful Vote Up Awesomefile
if yourrequestJobDescription
contains: which is the case for you. The fact that you received aCould not split the file on multiple lines
error means that the data in your CSV file was transmitted to our API, but the line breaks weren't correct, e.g. if your CSV file contained: then we received Since you only have one level of tags, you can also directly pass them as JSON objects in therequestJobDescription
:- Spam
- Abuse
- Report
0 · Accept Answer Off Topic Insightful Vote Up AwesomePowershell rookie....
Hoping to resurrect this issue. I am also trying to import tags using Powershell. In my case, the tags ARE dependent, so I need to import from a file. It looks to me like the import from file was never made to work, instead the user included the content in the json.
I tried the same code in post 3, and I too get the:
Could not split the file on multiple lines. The file probably does not have proper End Of Line characters.","responseCode":666
Looks like the script is passing the file's directory info instead of its contents... See below.
------------------------- Here is the content of $file ------
PS C:\Users\Repetto> $file
Directory: C:\users\repetto
Mode LastWriteTime Length Name
---- ------------- ------
-a---- 2/11/2020 10:10 AM 362446 ExpensifyTags.csv
------------------------ and the content of $encode ---------------
PS C:\Users\Repetto> $encode
C%3a%5cusers%5crepetto%5cExpensifyTags.csv
- Spam
- Abuse
- Report
0 · Accept Answer Off Topic Insightful Vote Up AwesomeSo maybe I am looking at this wrong. When I UrlEncode, is it the filename or the file contents I am trying to encode?
When I query the $body variable, I see this:
PS C:\Users\Repetto> $body
requestJobDescription={
"type": "update",
"credentials": {
"partnerUserID": "*********",
"partnerUserSecret": "********"
},
"inputSettings": {
"type": "policy",
"policyIDList": [
"***********"
]
},
"tags": {
"config": {
"setRequired": true,
"glCodes": false,
"header": true,
"dependency": true,
"fileType": "csv"
},
"source": "file",
"action": "replace"
}
}'&file='C%3a%5cusers%5crepetto%5cExpensifyTags.csv
- Spam
- Abuse
- Report
0 · Accept Answer Off Topic Insightful Vote Up AwesomeHi there,
It looks like you're only passing the path to the CSV file in the request, instead of the file's content.
Have you tried using
Get-Item
and[System.Web.HttpUtility]::UrlEncode($file)
like in the posts above?- Spam
- Abuse
- Report
0 · Accept Answer Off Topic Insightful Vote Up AwesomeI’m on a Mac using curl and also have this issue “could not split bla....”
did anyone find the solution?
Thanks
- Spam
- Abuse
- Report
0 · Accept Answer Off Topic Insightful Vote Up Awesome