1

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?

Dead-Red
  • 21
  • 4

2 Answers2

1

Thank's for your answer Charg, but when i try there are lot of change and pages. I have try this option before post here :D. Because for me the copy is not useable. Maybe easiest for you :

I have to create a new reply because the comment does not allow enough characters.

(i have deleted headers)

Invoke-WebRequest -Uri "https://glpi.XXXXXXXXXXXX.fr/ajax/fileupload.php" `
-Method "POST" `
-Headers @{

} `
-ContentType "multipart/form-data; boundary=----WebKitFormBoundarysgETZNBiXoMdeht2Q" `
-Body ([System.Text.Encoding]::UTF8.GetBytes("------WebKitFormBoundarysgETZNBiXoMdeht2Q$([char]13)$([char]10)Content-Disposition: form-data; name=`"name`"$([char]13)$([char]10)$([char]13)$([char]10)filename$([char]13)$([char]10)------WebKitFormBoundarysgETZNBiXoMeht2Q$([char]13)$([char]10)Content-Disposition: form-data; name=`"showfilesize`"$([char]13)$([char]10)$([char]13)$([char]10)1$([char]13)$([char]10)------WebKitFormBoundarysgETZNBiXoMeht2Q$([char]13)$([char]10)Content-Disposition: form-data; name=`"filename[]`"; filename=`"apple.png`"$([char]13)$([char]10)Content-Type: image/png$([char]13)$([char]10)$([char]13)$([char]10)$([char]13)$([char]10)------WebKitFormBoundarysgETZNBiXoMeht2Q--$([char]13)$([char]10)"))

And other body (more informations) :

-Body ([System.Text.Encoding]::UTF8.GetBytes("------WebKitFormBoundarymPxXKSWBW70lLtX2 $([char]13)$([char]10)Content-Disposition: form-data; name=`"documentcategories_id`"$([char]13)$([char]10)$([char]13)$([char]10)10$([char]13)$([char]10)------WebKitFormBoundarymPxXKSWBW70lLtX2$([char]13)$([char]10)Content-Disposition: form-data; name=`"entities_id`"$([char]13)$([char]10)$([char]13)$([char]10)2$([char]13)$([char]10)------WebKitFormBoundarymPxXKSWBW70lLtX2$([char]13)$([char]10)Content-Disposition: form-data; name=`"is_recursive`"$([char]13)$([char]10)$([char]13)$([char]10)0$([char]13)$([char]10)------WebKitFormBoundarymPxXKSWBW70lLtX2$([char]13)$([char]10)Content-Disposition: form-data; name=`"itemtype`"$([char]13)$([char]10)$([char]13)$([char]10)Ticket$([char]13)$([char]10)------WebKitFormBoundarymPxXKSWBW70lLtX2$([char]13)$([char]10)Content-Disposition: form-data; name=`"items_id`"$([char]13)$([char]10)$([char]13)$([char]10)161$([char]13)$([char]10)------WebKitFormBoundarymPxXKSWBW70lLtX2$([char]13)$([char]10)Content-Disposition: form-data; name=`"tickets_id`"$([char]13)$([char]10)$([char]13)$([char]10)161$([char]13)$([char]10)------WebKitFormBoundarymPxXKSWBW70lLtX2$([char]13)$([char]10)Content-Disposition: form-data; name=`"_filename[0]`"$([char]13)$([char]10)$([char]13)$([char]10)6075d0cb639f08.69946676apple.png$([char]13)$([char]10)------WebKitFormBoundarymPxXKSWBW70lLtX2$([char]13)$([char]10)Content-Disposition: form-data; name=`"_prefix_filename[0]`"$([char]13)$([char]10)$([char]13)$([char]10)6075d0cb639f08.69946676$([char]13)$([char]10)------WebKitFormBoundarymPxXKSWBW70lLtX2$([char]13)$([char]10)Content-Disposition: form-data; name=`"_tag_filename[0]`"$([char]13)$([char]10)$([char]13)$([char]10)ea6f951c-f37490bd-6075d0cb7fc703.58112843$([char]13)$([char]10)------WebKitFormBoundarymPxXKSWBW70lLtX2$([char]13)$([char]10)Content-Disposition: form-data; name=`"filename[]`"; filename=`"`"$([char]13)$([char]10)Content-Type: application/octet-stream$([char]13)$([char]10)$([char]13)$([char]10)$([char]13)$([char]10)------WebKitFormBoundarymPxXKSWBW70lLtX2$([char]13)$([char]10)Content-Disposition: form-data; name=`"add`"$([char]13)$([char]10)$([char]13)$([char]10)Ajouter un nouveau fichier$([char]13)$([char]10)------WebKitFormBoundarymPxXKSWBW70lLtX2$([char]13)$([char]10)Content-Disposition: form-data; name=`"_glpi_csrf_token`"$([char]13)$([char]10)$([char]13)$([char]10)583040be8c019c6b482ff61d4c6c37230109cb37876001d086603c1b4a144eb0$([char]13)$([char]10)------WebKitFormBoundarymPxXKSWBW70lLtX2--$([char]13)$([char]10)"))

And I found a guy, who wrote this part in Python (maybe help to translate to powershell ?)

multipart_form_data = {
    'uploadManifest': (None, '{"input": {"name": "test123.png", "_filename": ["test123.png"]}}'),
    'file': ('test123.png', open('test123.png', 'rb')),
}

And one other guy find his solution in C# (i have losted the post id)

// Upload

var RSClient = new RestClient(Properties.Settings.Default.GLPI_URL);

var request = new RestRequest("Document", Method.POST);
request.AddHeader("Session-Token", Properties.Settings.Default.GLPI_SESSION_TOKEN);
request.AddHeader("App-Token", Properties.Settings.Default.GLPI_APP_TOKEN);
request.AddHeader("Accept", "application/json");
request.AddHeader("Content-Type", "multipart/form-data");
request.AddQueryParameter("uploadManifest", "{\"input\": {\"name\": \"UploadFileTest\", \"_filename\": \"GiletsJaunes.jpg\"}}");
request.AddFile("test", @"C:\path\to\File.jpg");

IRestResponse response = RSClient.Execute(request);
var content = response.Content;

textBox2.Text = textBox2.Text + Environment.NewLine + content;
Dead-Red
  • 21
  • 4
0
  1. press f12 in chrome to open devtools, go to network tab (probably good idea to enable XHR filter too)
  2. manually upload file - new requests will show up in devtools
  3. find the one that uploads file
  4. right click - copy - as powershell

that will copy entire request that you can then adjust/review to have better idea of what goes where

Charg
  • 1
  • 1