Announcement

Collapse
No announcement yet.

Try to batch import files using REST API

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Try to batch import files using REST API

    Hi,
    I try to test the batch import of files (Documents) using the REST API.
    I read a previous post and the REST API page provided to try to find out how to import files.

    So I think that I have to create Attachment and then Document objects.

    I tried to create an attachment by using a POST request on URI https://myserver/api/v1/Attachment using the following JSON :
    {
    "size": 17620,
    "name": "name.pdf",
    "file": "<A lot of stuff coded in Base64 here >",
    "deleted": 0,
    "type": "application/pdf"
    }

    I get no Id of the attachment created (NULL return). There is a new row created in attachment table but there is no size, name, deleted or type provided (all these columns are NULL).

    I am sure I miss something in the process.

    Could you give me some infos about Attachment creation? Is there an API guide available?

    Thanks for your help!

  • #2
    Hello,
    check our documentation. Hope, you will find useful things: https://www.espocrm.com/documentation/development/api/. More information for development you can find here https://www.espocrm.com/documentatio...lopment/index/

    Comment


    • #3
      Thanks for your answer.

      I have already seen those pages, but I could not find any comprehensive API documentation.

      I tried to copy/paste a document creation request (using developpement view of Chrome) but the result remain the same.
      The GET request on objects are working fine.
      The user I used has admin rights.

      Is there any log to see why my request is not correctly handled?

      Comment


      • #4
        I just found the solution!
        I just need to add the -ContentType "application/json" type to the Invoke-WebRequest cmd!

        Here is a bunch of code for those who are interrested in using Powershell for REST API.

        Code:
        $server = "https://monserver/"
        $api = $server + "api/v1/"
        $username = "user"
        $password = "password" | ConvertTo-SecureString -asPlainText -Force
        $credential = New-Object System.Management.Automation.PSCredential($username, $password)
        $filename = "C:\temp\test.pdf"
        
        $Content = Get-Content -Path $fileName -Encoding Byte
        $Base64 = [System.Convert]::ToBase64String($Content)
        
        $cmd = $api + "Attachment"
        
        $cmd
        
        $param = @{
            field = "file";
            file = "data:application/pdf;base64,"+$Base64;
            name = "test.txt";
            relatedType = "Document";
            role = "Attachment";
            size = $Base64.Length;
            type = "application/pdf"
        } | ConvertTo-Json
        
        
        $return = Invoke-WebRequest -ContentType "application/json" -UseBasicParsing $cmd -Method POST -Credential $credential -Body $param -Verbose 
        
        "return value =" + $return

        Comment


        • patoban
          patoban commented
          Editing a comment
          Hi Toto

          Thank you so much for posting this. I have been trying for days to get this working.

          Tell me, have you managed to create a Document record using the API after the attachment has been created?

          Thanks

          Andrew
      Working...
      X