Has anyone performed a POST with the API using PowerShell?

I have a script to read field numbers from a table, map these to CSV columns, determine if the record exists or not, then POST either a new record or an update to an existing record.
All the GET queries to the API work, but my efforts to POST with PowerShell fail with
Invoke-RestMethod : {“type”:“error”,“msg”:“The request is invalid. That’s all we know.”}
I’ve tried simplifying all the way down to a single field update to a single record with the same error.
Can anyone point out where I might be going wrong? Below is the pertinent lines of code:

$headers = @{}
$headers.Add(“X-Tadabase-App-id”, $appID)
$headers.Add(“X-Tadabase-App-Key”, $apiKey)
$headers.Add(“X-Tadabase-App-Secret”, $apiSecret)
$headers.Add(“Content-Type”, “application/x-www-form-urlencoded”)

$multipartContent = [System.Net.Http.MultipartFormDataContent]::new()
$stringHeader = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new(“form-data”)
$dateFieldId = ($tableFields.fields | ?{$_.name -eq ‘Last Updated’}).slug
$stringHeader.Name = $dateFieldId
$stringContent = [System.Net.Http.StringContent]::new((Get-Date -Format “dd/MM/yyyy HH:mm”))
$stringContent.Headers.ContentDisposition = $stringHeader
$multipartContent.Add($stringContent)
Invoke-RestMethod “https://api.tadabase.io/api/v1/data-tables/$userDetailsTableID/records$specificRecordPath” -Method ‘POST’ -Headers $headers -Body $multipartContent

[DBG]: PS C:\Users\User>> “https://api.tadabase.io/api/v1/data-tables/$userDetailsTableID/records$specificRecordPath
https://api.tadabase.io/api/v1/data-tables/q3kjZ2Vj6V/records

[DBG]: PS C:\Users\User>> $multipartContent.ReadAsStringAsync()
Result --73660bbf-ad8d-4fd2-80f7-864c20bacda7
Content-Type: text/plain; charset=utf-8
Content-Disposition: form-data; name=“field_712”

                     13/09/2021 17:19
                     --73660bbf-ad8d-4fd2-80f7-864c20bacda7--

Id : 28747
Exception :
Status : RanToCompletion
IsCanceled : False
IsCompleted : True
CreationOptions : None
AsyncState :
IsFaulted : False
AsyncWaitHandle : System.Threading.ManualResetEvent
CompletedSynchronously : False

I’m not familiar with PowerShell but I took a look and got this POST code to work for me:

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("X-Tadabase-App-id", "YOURID")
$headers.Add("X-Tadabase-App-Key", "YOURKEY")
$headers.Add("X-Tadabase-App-Secret", "YOURSECRET")

$body = @{'field_121'='Test Field 1From Powershell' 
'field_122'='Test Field 2 From PowerShell'}

$response = Invoke-RestMethod -Uri 'https://api.tadabase.io/api/v1/data-tables/YOUR_TABLE_ID/records' -Method Post -Headers $headers -Body $body -ContentType "application/x-www-form-urlencoded"

$response | ConvertTo-Json

I did get the same error as you and I used webhook.site to troubleshoot it. I was able to see what the incoming request looked like and it appeared the issue was with the form values.

Also for your use case, you might consider using an incoming webhook (in beta) that can do this for you.

Moe, thank you!! So basically instead of the [System.Net.Http.MultipartFormDataContent] object that is complicated to instantiate with header data, the body can just be a standard PoSh Dictionary object (like the $headers variable) - that’s so easy :smile:
I trust you’ll update the incorrect info for the PoSh examples in the API documentation pages Tadabase API ? Thanks :wink:

Yes, I’ll take a look at the API Docs. The code is actually all dynamically generated by Postman, all we had to do is add the details of the API and all the code gets auto generated. I’ll see if its possible to tweak that code.