Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

BUG: Upload a Document file using addItems() fails #76

Open
yorenet opened this issue May 21, 2020 · 5 comments
Open

BUG: Upload a Document file using addItems() fails #76

yorenet opened this issue May 21, 2020 · 5 comments

Comments

@yorenet
Copy link

yorenet commented May 21, 2020

Observed Results:

It is not possible to upload files using addItems() method because of the following wrong assumptions:

  • addItems() method of ItemHandler class assumes that queryString is input parameter
  • request() method of Client class forces Content-type as 'application/json'

Expected behavior:

Code should respect for 'multipart/data' content_type

@yorenet
Copy link
Author

yorenet commented May 22, 2020

I have a fix for the issue but don't have right to push:

$ git push origin yorenet-fix-issue76
remote: Permission to glpi-project/php-library-glpi.git denied to yorenet.
fatal: unable to access 'https://github.com/glpi-project/php-library-glpi.git/': The requested URL returned error: 403

@yorenet
Copy link
Author

yorenet commented May 22, 2020

Here is the fix:

$ git diff develop
diff --git a/src/Glpi/Api/Rest/Client.php b/src/Glpi/Api/Rest/Client.php
index c5357bb..875d447 100644
--- a/src/Glpi/Api/Rest/Client.php
+++ b/src/Glpi/Api/Rest/Client.php
@@ -175,7 +175,8 @@ class Client {
    public function request($method, $uri, array $options = []) {
       $apiToken = $this->addTokens();
       try {
-         $options['headers']['Content-Type'] = "application/json";
+             if(array_key_exists('multipart', $options) == false)
+                  $options['headers']['Content-Type'] = "application/json";
          $sessionHeaders = [];
          if ($apiToken) {
             if (key_exists('Session-Token', $apiToken) && $apiToken['Session-Token']) {
diff --git a/src/Glpi/Api/Rest/ItemHandler.php b/src/Glpi/Api/Rest/ItemHandler.php
index 90e7109..da262c2 100644
--- a/src/Glpi/Api/Rest/ItemHandler.php
+++ b/src/Glpi/Api/Rest/ItemHandler.php
@@ -525,7 +525,10 @@ class ItemHandler {
     * @return array
     */
    public function addItems($itemType, array $queryString) {
-      $options['body'] = json_encode(['input' => $queryString]);
+          if(array_key_exists('multipart', $queryString))
+         $options=$queryString;
+      else
+         $options['body'] = json_encode(['input' => $queryString]);
       $response = $this->client->request('post', $itemType . '/', $options);
       $location = $response->getHeader('location');
       $link = $response->getHeader('Link');

@Stoufiler
Copy link

@yorenet Hello, thanks for the fix, but i can't send Document do you have an idea ?

@yorenet
Copy link
Author

yorenet commented Sep 29, 2020

@yorenet Hello, thanks for the fix, but i can't send Document do you have an idea ?

Sure. Here is a sample code to upload a file after the fix:

/* assumptions
We are adding an attachment to an existing Ticket
and simulating file upload via HTML form

$cid: ID of the Ticket to add attachment
$itemhandler: \Glpi\Api\Rest\ItemHandler object
$filepath: path/to/the/file/to/upload
$docname: Description of the file
$filename: given name to the uploaded file
'inputName[]': name of the <input ....> node

*/

$query= [
	'multipart' => [
		// the document part
		[
			'name'     => 'uploadManifest',
			'contents' => json_encode([
				'input' => [
					'name'       => $docname,
					'_filename'  => [$filename],
					'itemtype'  => 'Ticket',
					'items_id'  => $cid
				]
			])
		],
		// the FILE part
		[
			'name'     => 'inputName[]',
			'contents' => file_get_contents($filepath),
			'filename' => $filename
		]
	]
];

$rv=$itemHandler->addItems('Document', $query);

/*
if ($rv['statusCode'] >= 200 && $rv['statusCode'] < 300) then success
otherwise ERROR
*/

@Stoufiler
Copy link

Thanks a lot @yorenet it works very well !!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants