Answers.Body: <p>Sorry, this is a half-answer.</p>
  
  <p>The HTML encoding is being added because <a href="https://github.com/civicrm/civicrm-core/blob/master/Civi/API/Subscriber/WrapperAdapter.php#L101" rel="nofollow noreferrer">this line</a> is adding default 'wrapper' functions that include adding HTML entities (<code>CRM_Utils_API_HTMLInputCoder</code>).</p>
  
  <p>You can fix this by altering the defaults passed into <code>WrapperAdapter::construct()</code>, to include <code>'content'</code> in the array of <code>skipFields</code>. I think you could do this <a href="https://github.com/civicrm/civicrm-core/blob/master/CRM/Utils/API/HTMLInputCoder.php#L69" rel="nofollow noreferrer">here</a>.</p>
  
  <p>Just add a line like</p>
  
  <pre><code>'content', // For attachment.create
  </code></pre>
  
  <p>This does work - I tried it.</p>
  
  <p><strong>However this may not be secure</strong> because I don't know if any of the other APIs use the rather generic 'content' parameter name. If they do, then removing the protection afforded could result in XSS vulnerabilities or other problems.</p>
  
  <p>So the other way to fix it would be rename the API <code>content</code> something more unique <code>attachment_content</code> - but that would require changing a public API (!)</p>
  
  <p>So another way would be to put in some sort of check that changes the default wrappers for the particular case of <code>attachment.create</code>, but I'm not familiar enough with the internal workings of the API system to suggest that patch.</p>
  
  <p>Hmmm.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-01-15T12:17:18.413Z
Answers.LastActivityDate: 2018-01-15T12:17:18.413Z
Answers.UserDisplayName: artfulrobot
Answers.Body: <p>thank you for answer me! i've resolved in this way:</p>
  
  <ol>
  <li>I've created file with pure PHP</li>
  <li>Then i've used <strong>civicrm_api3('Attach',...)</strong> with 'options.move-file'.</li>
  </ol>
  
  <p><strong>options.move-file</strong> is used to attach to civicrm_file record the previously created file.</p>
  
  <p>Maybe isn't the best solution, but works!</p>
  
  <p>Thank you!</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-01-23T21:50:48.650Z
Answers.LastActivityDate: 2018-01-23T21:50:48.650Z
Answers.UserDisplayName: Nino Marrazzo
Body: <p>i need help, i'm trying to attach through <strong>civicrm_api3('Attach', 'create'..)</strong> a <strong>pdf</strong> file, passed with a rest call as simply text:</p>
  
  <pre><code>%PDF-1.3
  1 0 obj
  &lt;&lt; /Type /Catalog
  ... ecc
  </code></pre>
  
  <p>.
  The attach works, but i thing that during it, the crm's file save function make an escape of some special chars... the results of content of pdf file is:</p>
  
  <pre><code>%PDF-1.3
  1 0 obj
  &amp;lt;&amp;lt; /Type /Catalog
  ... ecc
  </code></pre>
  
  <p>there is a way to avoid this behavior?</p>
  
  <p>thanks</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-01-14T14:45:01.873Z
Comments.Text: I'm trying to find another solution to programmatically attach a file to custom field through API... but for now i haven't found it.
Comments.UserDisplayName: Nino Marrazzo
Tags: <custom-fields><api><civicrm-entity>
Title: Attach pdf file civicrm_api3
ThreadText: # Attach pdf file civicrm_api3
  
  (Asked by: username_0 on 2018-01-13)
  
  i need help, i'm trying to attach through civicrm_api3('Attach', 'create'..) a pdf file, passed with a rest call as simply text:
  %PDF-1.3
  1 0 obj
  << /Type /Catalog
  ... ecc
  
  .
  The attach works, but i thing that during it, the crm's file save function make an escape of some special chars... the results of content of pdf file is:
  %PDF-1.3
  1 0 obj
  << /Type /Catalog
  ... ecc
  
  there is a way to avoid this behavior?
  thanks
  
  **Tags:** custom-fields, api, civicrm-entity
  
  > **Comments:**
  > **username_0**: I'm trying to find another solution to programmatically attach a file to custom field through API... but for now i haven't found it.  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2018-01-15. Score: 1
  
  Sorry, this is a half-answer.
  The HTML encoding is being added because this line is adding default 'wrapper' functions that include adding HTML entities (CRM_Utils_API_HTMLInputCoder).
  You can fix this by altering the defaults passed into WrapperAdapter::construct(), to include 'content' in the array of skipFields. I think you could do this here.
  Just add a line like
  'content', // For attachment.create
  
  This does work - I tried it.
  However this may not be secure because I don't know if any of the other APIs use the rather generic 'content' parameter name. If they do, then removing the protection afforded could result in XSS vulnerabilities or other problems.
  So the other way to fix it would be rename the API content something more unique attachment_content - but that would require changing a public API (!)
  So another way would be to put in some sort of check that changes the default wrappers for the particular case of attachment.create, but I'm not familiar enough with the internal workings of the API system to suggest that patch.
  Hmmm.
  
  
  ---
  
  ### Answer by username_0 on 2018-01-23. Score: 0
  
  thank you for answer me! i've resolved in this way:
  
  1. I've created file with pure PHP
  2. Then i've used **civicrm\_api3('Attach',...)** with 'options.move-file'.
  
  **options.move-file** is used to attach to civicrm\_file record the previously created file.
  
  Maybe isn't the best solution, but works!
  
  Thank you!
  
  
  ---
  
  