POST the Attachment with API

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • laoris
    Junior Member
    • Feb 2020
    • 1

    POST the Attachment with API

    I'm trying to post file in Attachment. Aсcording to other topics: i need to send json with file encoded to base64. I found some php code, but it doesn't work, even php. When i remove field "file", response - 200, but there is no any file attached.
    My python code also fail in this field.

    Example php:
    $postFields = json_encode(array(
    'name' => 'tr.txt',
    'publishDate' => date('Y-m-d'),
    'type' => 'application/octet-stream',
    'size' => 356297,
    'file' => ',' . base64_encode(file_get_contents('F:\project\common \tr.txt')),
    ));

    Response:
    HTTP/1.1 400 Bad Request
    Access-Control-Allow-Methods: POST, GET, PUT, PATCH, DELETE
    X-Powered-By: PHP/7.1.33
    X-Status-Reason: Params 'field' and 'parentType' not passed along with 'file'.
    Content-Length: 0
    Connection: close
    Content-Type: text/html;charset=UTF-8

    But as the result, i must write it on python.
    Last edited by laoris; 02-07-2020, 12:35 PM.
  • yuri
    Member
    • Mar 2014
    • 8455

    #2
    I recommend to open browser console F12 > network tab and post attachment (from EspoCRM). You will see which fields you need to send. I believe you need to use 'contents' instead of 'file'.
    If you find EspoCRM good, we would greatly appreciate if you could give the project a star on GitHub. We believe our work truly deserves more recognition. Thanks.

    Comment

    • laoris
      Junior Member
      • Feb 2020
      • 1

      #3
      'contents' helped - the file is attached!
      but now it is broken and no view

      in php:
      $postFields = json_encode(array(
      'contents' = ',' . base64_encode(file_get_contents('F:\project\common \tr.txt')),
      ));

      in python:
      with open('s.pdf', 'rb') as file:
      f = f',{(base64.b64encode(file.read()))}'
      and so on.. json.dumps()..
      Results are equal.
      Last edited by laoris; 02-11-2020, 08:38 AM.

      Comment

      • laoris
        Junior Member
        • Feb 2020
        • 1

        #4
        I try to send file using this function:

        Code:
        with open('s.pdf', 'rb') as file:
        f = f'data:application/pdf;base64,{base64.b64encode(file.read()).decode() }'
        
        {"name": "s.pdf", "type": "application/pdf", "role": "Attachment", "size": 356297, "field": "attachments", "contents": f}
        But still attached file is unreadable(
        The value of "contents" similar to the value of "file" from brousers request. And i realy don't know what to do(

        Click image for larger version  Name:	attach.jpg Views:	2 Size:	30.9 KB ID:	55884.

        Click image for larger version

Name:	t.jpg
Views:	783
Size:	15.9 KB
ID:	55886
        Last edited by laoris; 02-11-2020, 09:00 AM.

        Comment

        • esendino
          Senior Member
          • May 2019
          • 158

          #5
          Does anyone know how to solve this?

          As other persons I'm getting the message: Params 'field' and 'parentType' not passed along with 'file'.

          I tried many things but unable to solve it

          Comment

          Working...