0

I would like to convert the following command:

$ curl -X POST \
-H 'Content-Type: multipart/form-data' \
-H "Session-Token: [Session Token]" \
-H "App-Token: [MyToken] " \
-F 'uploadManifest={"input": {"name": "Uploaded document", "_filename" : ["file.txt"]}};type=application/json' \
-F 'filename[0]=@file.txt' \
'http://path/to/glpi/apirest.php/Document/'

Using the Invoke-RestMethod command. I follow arrived at the following result, but impossible to understand how to pass this part to this :

"-F 'uploadManifest={"input": {"name": "Uploaded document", "_filename" : ["file.txt"]}};type=application/json' \"

My result :

$header = @{} ;
$header['Session-Token'] = $session
$header['App-Token'] = $apptoken
$header['Accept'] = "application/json"
$header['Content-Type'] = "multipart/form-data"

$body_input = @{}
$body_input["name"] = "My Document"
$body_input["_filename"] = "MyDoc.txt"

$InputObjectTab = @{} ;
$InputObjectTab."input" = $body_input ;
$JsonItem = ConvertTo-Json $InputObjectTab

$FilePath = "‪C:\Users\MyUser\Downloads\test.txt" ;
$uri = "$GlpiUri/apirest.php/Document"
$GlpiDocument      = Invoke-RestMethod -uri $uri -Method Post  -Headers $header -ContentType 'image/jpg' -InFile $FilePath

As you understand, this command allows you to upload a file using a REST API

Any ideas ?

  • 1
    GLPI API? There's PowerShell module for it, and the sources illustrate how to upload files - what's apparently missing in your code is reading the file bytes. Check out the GLPI Tools repo (https://github.com/wpietrzakpl/GlpiTools) and particularly this source file: https://github.com/wpietrzakpl/GlpiTools/blob/master/Public/APICore/Add%20Item/Add-GlpiToolsItems.ps1 – Svyatoslav Pidgorny Jan 20 '20 at 23:36
  • Hey Pidgorny, thank for this repply, i don't understand how use it for upload a file :D and this not respond to my question :D. Any other idea ? My issu is it : How upload a file to GLPI document using Invoke-rest method ( or other powershell method ) – Florian Guerin Jan 26 '20 at 20:52

0 Answers0