EspoCRM API Attachment empty response

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kanoriuta
    Junior Member
    • Jan 2017
    • 11

    EspoCRM API Attachment empty response

    Hello,

    I'm trying to upload contact's document via EspoCRM API v1.

    The first thing I do is send POST fields to 'api/v1/Attachment'. My POST fields look like this:
    PHP Code:
    CURLOPT_POSTFIELDS => json_encode(array(
        'name' => $_FILES['file']["name"],
        'publishDate' => date('Y-m-d'),
        'type' => $_FILES['file']['type'],
        'size' => $_FILES['file']['size'],
        'file' => ',' . base64_encode(file_get_contents($_FILES['file']['tmp_name'])),
    )), 
    
    File is being created but unfortunately, I don't get any response from API so I just don't know which ID should be assigned with the contact.

    Any suggestions how to get new attachment's ID from API?
    Last edited by kanoriuta; 01-19-2017, 02:31 PM.
  • tanya
    Senior Member
    • Jun 2014
    • 4308

    #2
    Hello.
    It returns the ID.
    Attached Files

    Comment

    • kanoriuta
      Junior Member
      • Jan 2017
      • 11

      #3
      Hello,
      It returns the ID then I upload .txt files, but try adding pdf or image files and it won't return any data.
      Tried it with your script and the same result happened.
      I am using 4.3.1 version by the way.

      Comment

      • tanya
        Senior Member
        • Jun 2014
        • 4308

        #4
        Better to use $url = "ESPO_DIR/api/v1/Attachment/action/upload";
        And you will get attachmentId in response
        Last edited by tanya; 01-23-2017, 09:44 AM.

        Comment

        • kanoriuta
          Junior Member
          • Jan 2017
          • 11

          #5
          I changed the url and I got the attachment ID.
          Unfortunately in localhost/431/data/upload/ empty files are being created now.
          The same thing with database table `attachment` only the `ID`, `created_at` and `created_by_id` were added, other values are empty.

          Comment

          • tanya
            Senior Member
            • Jun 2014
            • 4308

            #6
            read actionUpload here application/Espo/Controllers/Attachment.php

            set all field (except file) in one more patch request.

            Comment

            • kanoriuta
              Junior Member
              • Jan 2017
              • 11

              #7
              application/Espo/Controllers/Attachment.php ln 47: $arr = explode(',', $data);
              application/Espo/Services/Attachment.php ln 39: list($prefix, $contents) = explode(',', $data['file']);
              One of them is exploding $data and the other one $data['file']

              I'm still not sure why that comma is so necessary or why it can't be done with just one POST request. But it must be a bug because it can be done with one POST request while uploading text files.

              So in the end I sent POST request to 'localhost/espo/api/v1/Attachment/action/upload' with fields:
              PHP Code:
              CURLOPT_POSTFIELDS => json_encode(
                  ',' . base64_encode(file_get_contents($_FILES['file']['tmp_name']))
              ), 
              
              And then PATCH request to 'localhost/espo/api/v1/Attachment/{attachment_id}' with fields:
              PHP Code:
              CURLOPT_POSTFIELDS => json_encode(array(
                  'name' => $_FILES['file']["name"],
                  'publishDate' => date('Y-m-d'),
                  'type' => $_FILES['file']['type'],
                  'size' => $_FILES['file']['size'],
              )), 
              
              And it's all good now.

              Comment

              • kanoriuta
                Junior Member
                • Jan 2017
                • 11

                #8
                One more question related with attachments.

                After attachment creation I sent new POST request to 'localhost/espo/api/v1/Document' and I got document's ID.

                Now I need to insert new data into 'contact_document' table but I don't know how.
                Is it even possible to connect contact and document via API?

                Or maybe I should create new topic for it?

                Comment

                • tanya
                  Senior Member
                  • Jun 2014
                  • 4308

                  #9
                  The easiest way to get answer, if you do not want to read a documentation ( https://www.espocrm.com/documentation/development/api/ Related Entities), is installing test espocrm instanse. Then you do what you want with UI and check Network Log in browser console

                  Request URL: ESPO_LOCATION/api/v1/Document/{documentId}/contacts
                  Request Method: POST
                  Post Params: {ids: ["{contactId}"]}

                  OR

                  Request URL: ESPO_LOCATION/api/v1/Contact/{contactId}/documents
                  Request Method: POST
                  Post Params: {ids: ["{documentId1}", "{documentId2}","{documentId3}"]}

                  Comment


                  • Maryam123
                    Maryam123 commented
                    Editing a comment
                    Why no one is giving response to my post
                Working...