You are not logged in.

Announcement

 Téléchargez la dernière version stable de GLPI      -     Et vous, que pouvez vous faire pour le projet GLPI ? :  Contribuer
 Download last stable version of GLPI                      -     What can you do for GLPI ? :  Contribute

#1 2019-10-28 22:32:50

mecmav
Member
From: Brasil
Registered: 2019-03-22
Posts: 326

Uploading document or file through API

Hello again to all

I trying to upload a file to GLPI, I've already did initSession, working fine.

I'm Using PostMan to do that. Reading docs i need set uploadManifest and value {"input": {"name": "Subindo docs", "_filename" : ["C:/teste.txt"]}};type=application/json' but I get this response: [
    "ERROR_UPLOAD_FILE_TOO_BIG_POST_MAX_SIZE",
    "The file seems too big"
]

Another way I try use a lib from https://github.com/orthagh/glpi_api_upload and put on my glpi.
When I upload a doc, nothing happens, the screen empty data.

How can I use the above explanation?

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

< 201 OK
< Location: http://path/to/glpi/api/Document/1
< {"id": 1, "message": "Document move succeeded.", "upload_result": {...}}

Offline

#2 2019-10-29 13:53:00

mecmav
Member
From: Brasil
Registered: 2019-03-22
Posts: 326

Re: Uploading document or file through API

I have found a error in my request and now working fine.

Offline

#3 2020-01-23 15:17:16

ben2249
Member
From: FRANCE
Registered: 2019-12-03
Posts: 35

Re: Uploading document or file through API

Hello,

What was your error ?

Offline

#4 2020-01-23 17:00:39

mecmav
Member
From: Brasil
Registered: 2019-03-22
Posts: 326

Re: Uploading document or file through API

ben2249 wrote:

Hello,

What was your error ?

In the Postman, I miss set multipart/form-data in the request

Offline

#5 2020-01-23 17:01:19

mecmav
Member
From: Brasil
Registered: 2019-03-22
Posts: 326

Re: Uploading document or file through API

What's happening with your request try?

Offline

#6 2020-01-23 17:03:39

ben2249
Member
From: FRANCE
Registered: 2019-12-03
Posts: 35

Re: Uploading document or file through API

I have translated the request in php curl and got the same error, although I know this is not a size issue..

Offline

#7 2020-01-23 17:28:20

mecmav
Member
From: Brasil
Registered: 2019-03-22
Posts: 326

Re: Uploading document or file through API

ben2249 wrote:

I have translated the request in php curl and got the same error, although I know this is not a size issue..

Really, this is not error from the size of the file.

Review your request, if you're using Postman, set uploadManifest, this error gone, At least for me.

Try put your code, maybe I can help you.

Offline

#8 2021-04-13 20:55:00

Dead-Red
Member
From: France - Aisne - 02
Registered: 2021-04-10
Posts: 20

Re: Uploading document or file through API

Hello mecmav,

I am trying to upload document from powershell but unable to find the good syntax.

And i would like to try learn more from postman, could you help me (and us) and said me how put theses settings  to uploaded document from postman ?

Thank's for your help.

Offline

#9 2021-04-13 22:05:56

mecmav
Member
From: Brasil
Registered: 2019-03-22
Posts: 326

Re: Uploading document or file through API

Dead-Red wrote:

Hello mecmav,

I am trying to upload document from powershell but unable to find the good syntax.

And i would like to try learn more from postman, could you help me (and us) and said me how put theses settings  to uploaded document from postman ?

Thank's for your help.


what error you are getting?

With Postman, you can simulate your requests with data that you use in glpi.

Try put a printscreen of your error here

Offline

#10 2021-04-13 23:36:06

Dead-Red
Member
From: France - Aisne - 02
Registered: 2021-04-10
Posts: 20

Re: Uploading document or file through API

Thank's for your time smile

I have created my account only this day tongue
Postman-Setdoc01.png

And body  (with error)

Postman-Setdoc02.png

And i don't know if i must put anything in Tests ?

(Thank's you again)

Offline

#11 2021-04-14 14:30:39

mecmav
Member
From: Brasil
Registered: 2019-03-22
Posts: 326

Re: Uploading document or file through API

Dead-Red wrote:

Thank's for your time smile

I have created my account only this day tongue
https://i.ibb.co/zNNfsS6/Postman-Setdoc01.png

And body  (with error)

https://i.ibb.co/xsXY0mP/Postman-Setdoc02.png

And i don't know if i must put anything in Tests ?

(Thank's you again)

Hello,

Are you trying to upload a single document to glpi, or you trying upload a document and attach to a ticket??

Offline

#12 2021-04-14 19:42:01

Dead-Red
Member
From: France - Aisne - 02
Registered: 2021-04-10
Posts: 20

Re: Uploading document or file through API

I have trying both sad

And in this capture i try to upload document to ticket 161

Offline

#13 2021-04-15 04:53:39

mecmav
Member
From: Brasil
Registered: 2019-03-22
Posts: 326

Re: Uploading document or file through API

Dead-Red wrote:

I have trying both sad

And in this capture i try to upload document to ticket 161

For each of this, you need make first a request in API to insert a document file in glpi.

After that you need do another request to make the relationship between the document that was uploaded with the type of object you want (e.g ticket, problem, contract, etc).

Tomorrow I can explain these methods, you can call me at personal email.

My email it's the same as the my username here in the forum. The domain of my email is hotmail.

So my email is my username@hormail.com

I need say this way for prevent phishing or spamming crawlers found my email.

Offline

#14 2021-04-15 08:37:22

Jean-Christophe
Moderator
Registered: 2007-08-22
Posts: 734

Re: Uploading document or file through API

Do not go to email as I will lose the conversation :-)
Or come back with a solution :-D

Offline

#15 2021-04-16 16:47:31

mecmav
Member
From: Brasil
Registered: 2019-03-22
Posts: 326

Re: Uploading document or file through API

I did some tests, and working fine.

Using Postman I can insert a document and later relate this file to a ticket or any other object type.

Offline

#16 2021-04-17 09:17:32

Dead-Red
Member
From: France - Aisne - 02
Registered: 2021-04-10
Posts: 20

Re: Uploading document or file through API

Jean-Christophe wrote:

Do not go to email as I will lose the conversation :-)
Or come back with a solution :-D

Yes if i try with success to put a document with Postman i come back here to insert screens to help ohter.

And For your code from your module, i think we must try to add document and assign after it to ticket. (maybe api../ticket/document) does not work directly sad

mecmav wrote:

I did some tests, and working fine.

Using Postman I can insert a document and later relate this file to a ticket or any other object type.


Could you send, as me the sceenshots from the postman ? (or you want only by mail ?)

Offline

#17 2021-04-17 19:28:16

mecmav
Member
From: Brasil
Registered: 2019-03-22
Posts: 326

Re: Uploading document or file through API

You need have the files that you want to insert in glpi:

Here I have 2 files named (test.txt, teste.txt)
We will use any of them files.

In Postman, you will need make a first request to insert and create a file in glpi
For do that, you will need follow these settings:

Define the headers and body request
URL: yourinstance/apirest.php/Document
Use the Content-Type given by Postman (but review if is defined to multipart/form-data; boundary=<calculated...)

Your App-Token and Session-Token (here I assuming that you already did the initSession method)

Go to the Body Tab
Select "form-data" radio button
Inform the 2 parameters:

uploadManifest
filename

In the "uploadManifest" (must be text format) you send the input data (JSON format) for insert register in glpi database:

In the "filename"  (must be in file format), you select directly your file clicking in the button "Select File":



Now you need send your request and analyze the result:


Now you have the response. If a correct request was sent, you will need get "201 Created" status response.


In the GLPI, go to Management > Documents, and you will see the created item:

Offline

#18 2021-04-17 21:49:04

Dead-Red
Member
From: France - Aisne - 02
Registered: 2021-04-10
Posts: 20

Re: Uploading document or file through API

Thank's a lot !

My error was to define mine Content-Type in Headers.

To working, you must display the hidden Keys (add only the App-Token & Session-token)

Thank's you Mecmav ..!

Offline

#19 2023-08-17 12:48:50

gopal.rathod
Member
Registered: 2023-08-17
Posts: 1

Re: Uploading document or file through API

in POST data pass the json_encode string instead of Array.


$parameters["input"] = [
    "entities_id" => 0,
    "name" => "Printer ribbin issue",
    "content" => "comment from Spot's input",
];

$json_param = json_encode($parameters);

Offline

#20 2024-02-08 16:28:48

leolage
Member
Registered: 2020-10-28
Posts: 6

Re: Uploading document or file through API

Hello, after sucessfull upload a document, how can I set the documento to ticket?

I'm trying a post on:

app key and session key ok

ticket is 49069 and id of document is 14004

https://[site]/apirest.php/Ticket/49069/Document

{
    "input": {
        "items_id": "49069",
        "itemtype": "Ticket",
        "id": "14004"
    }
}


But I receive this error:

[
    "ERROR_GLPI_ADD",
    ""
]

Offline

#21 2024-02-08 16:45:38

cconard96
Moderator
Registered: 2018-07-31
Posts: 2,335
Website

Re: Uploading document or file through API

The current API is a little quirky.
/Ticket/{tickets_id}/Document actually refers to the "Document" item class, not documents linked to Tickets. The actual documents are represented by the "Document" class, while the links between Documents and other items/tickets is "Document_Item".
You may be able to do:
/Ticket/49069/Document_Item
{
    "documents_id": 14004
}

If not, this should definitely work:
/Document_Item
{
    "documents_id": 14004,
    "itemtype": "Ticket",
    "items_id": 49069
}


GLPI Collaborator and Plugin Developer.
My non-English comments are automated translations. Sorry for any confusion that causes.
Mes commentaires non anglais sont des traductions automatiques. Désolé pour toute confusion qui cause.
Mis comentarios que no están en inglés son traducciones automáticas. Perdón por cualquier confusión que cause.

Offline

#22 2024-02-08 17:06:45

leolage
Member
Registered: 2020-10-28
Posts: 6

Re: Uploading document or file through API

Thanks for the quick response, it worked here! I needed to do this action using the administrator token.

Is it possible to upload a file in a ticket interaction via API too? I tried searching this documentation or finding examples, but I didn't find anything.

When I finish here I will share my php script to help other users with the same needs as mine.

cconard96 wrote:

The current API is a little quirky.
/Ticket/{tickets_id}/Document actually refers to the "Document" item class, not documents linked to Tickets. The actual documents are represented by the "Document" class, while the links between Documents and other items/tickets is "Document_Item".
You may be able to do:
/Ticket/49069/Document_Item
{
    "documents_id": 14004
}

If not, this should definitely work:
/Document_Item
{
    "documents_id": 14004,
    "itemtype": "Ticket",
    "items_id": 49069
}

Offline

#23 2024-02-08 23:05:14

cconard96
Moderator
Registered: 2018-07-31
Posts: 2,335
Website

Re: Uploading document or file through API

If you haven't already, please check the API documentation for uploading a document.
/apirest.php#upload-a-document-file
This URL is relative to your GLPI URL.


GLPI Collaborator and Plugin Developer.
My non-English comments are automated translations. Sorry for any confusion that causes.
Mes commentaires non anglais sont des traductions automatiques. Désolé pour toute confusion qui cause.
Mis comentarios que no están en inglés son traducciones automáticas. Perdón por cualquier confusión que cause.

Offline

#24 2024-02-09 17:19:14

leolage
Member
Registered: 2020-10-28
Posts: 6

Re: Uploading document or file through API

Yes, I can upload a document.

I see 2 types of uploads on a ticket:
1 - upload document and link to ticket
2 - upload document inside interaction (on glpi interface), its possible do this upload inside post a interaction (followup)?

Image below with same image in different upload methods.


upload

cconard96 wrote:

If you haven't already, please check the API documentation for uploading a document.
/apirest.php#upload-a-document-file
This URL is relative to your GLPI URL.

Last edited by leolage (2024-02-09 17:21:39)

Offline

#25 2024-02-10 16:51:56

cconard96
Moderator
Registered: 2018-07-31
Posts: 2,335
Website

Re: Uploading document or file through API

leolage wrote:

2 - upload document inside interaction (on glpi interface), its possible do this upload inside post a interaction (followup)?

Yes. Instead of associating the document with the ticket, associate it with the followup after the followup is created.
itemtype: "ITILFollowup"
items_id: YOUR_FOLLOWUP_ID


GLPI Collaborator and Plugin Developer.
My non-English comments are automated translations. Sorry for any confusion that causes.
Mes commentaires non anglais sont des traductions automatiques. Désolé pour toute confusion qui cause.
Mis comentarios que no están en inglés son traducciones automáticas. Perdón por cualquier confusión que cause.

Offline

Board footer

Powered by FluxBB