我想从 API 上传一个文件到我的 GLPI。
这个 curl 命令工作正常:
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'
powershell 中的相同卷曲(感谢 GLPI 的 Jean-Christophe):
$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/'
我想使用 Invoke-RestMethod 但没有成功
# 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"}
调用:-ContentType 'multipart/data'
["ERROR_BAD_ARRAY","The input parameter must be an array of objects
调用:-ContentType 'multipart/form-data'
["ERROR_UPLOAD_FILE_TOO_BIG_POST_MAX_SIZE","The file seems too big"]
如果有人看到我的错误并帮助我,我提前感谢。