I would like to upload a file to my GLPI from API.
This curl command works fine :
curl -X POST
-H 'Content-Type: multipart/form-data'
-H 'Session-Token: $sessiontoken'
-H 'App-Token:$apptoken'
-F 'uploadManifest={"input": {"name": "Uploaded document", "_filename" : ["file.txt"]}};type=application/json'
-F 'filename[0]=@file.txt' 'http://GLPI_SERVER/glpi/apirest.php/Document'
The same curl in powershell (Thank's to Jean-Christophe from GLPI) :
$DocumentUpload = C:\curl\bin\curl.exe `
-X POST `
-H 'Content-Type: multipart/form-data' `
-H "Session-Token: $($SessionToken.session_token)" `
-H 'App-Token: '$AppToken `
-F 'uploadManifest={\"input\": {\"name\": \"Document ticket 161\", \"_filename\" : [\"clear.png\"], \"tickets_id\":\"161\"}};type=application/json' `
-F 'filename[0]=@"C:\temp1\clear.png"' `
-s `
-k 'https://glpi.xxxx.fr/apirest.php/Document/'
I would like use the Invoke-RestMethod but without success
# GLPI REST API CONFIG :
$AppURL = "https://glpi.xxxxxxxxxxxx.fr/apirest.php"
$UserToken = "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"
$AppToken = "XXXXXXXXXXX"
$ID = 161
$ItemType = "Ticket"
$SubItemType = "Document"
$manifest = @{}
$SessionToken = Invoke-RestMethod "$AppURL/initSession" -Method Get -Headers @{"Content-Type" = "application/json";"Authorization" = "user_token $UserToken";"App-Token"=$AppToken}
$FileContent = [System.IO.File]::ReadAllBytes("C:\temp1\clear.png")
$input = New-Object System.Collections.ArrayList
$type =New-Object System.Collections.ArrayList
$filename =New-Object System.Collections.ArrayList
$input = @{input=@{"name"="titre";"_filename"="clear.png";"tickets_id"="161"}}
$type = @{"type"="application/json"}
$filename =@{"filename[0]"="C:\temp1\clear.png"}
$manifest.Add("uploadManifest",$input)
$manifest.Add("type=","application/json")
$manifest.Add("filename[0]=",@("C:\temp1\clear.png"))
$json = @($manifest | ConvertTo-Json)
#Display Convert to JSON
#$json
Invoke-RestMethod "$AppURL/$($ItemType)/$($ID)/$($SubItemType)" -Method Post -Headers @{"session-token"=$SessionToken.session_token; "App-Token" = $AppToken} -Body ([System.Text.Encoding]::UTF8.GetBytes($json)) -ContentType 'multipart/data'
## Test autre ContentType
Invoke-RestMethod "$AppURL/$($ItemType)/$($ID)/$($SubItemType)" -Method Post -Headers @{"session-token"=$SessionToken.session_token; "App-Token" = $AppToken} -Body ([System.Text.Encoding]::UTF8.GetBytes($json)) -ContentType 'multipart/form-data'
#Kill session
Invoke-RestMethod "$AppURL/killSession" -Headers @{"session-token"=$SessionToken.session_token; "App-Token" = "$AppToken"}
The Invoke with : -ContentType 'multipart/data'
["ERROR_BAD_ARRAY","The input parameter must be an array of objects
The Invoke with : -ContentType 'multipart/form-data'
["ERROR_UPLOAD_FILE_TOO_BIG_POST_MAX_SIZE","The file seems too big"]
I thank in advance if someone sees my mistakes and help me.
Same topic to : How convert curl file upload command to Invoke-RestMethod?