# File/asset upload trough the API

**URL:** <https://discourse.getcockpit.com/t/file-asset-upload-trough-the-api/1208>\
**Category:** Support\
**Created:** [February 11, 2020, 4:45pm UTC](https://discourse.getcockpit.com/t/file-asset-upload-trough-the-api/1208 "2020-02-11T16:45:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Boerul](https://avatars.discourse-cdn.com/v4/letter/b/b5ac83/32.png) [@Boerul](https://discourse.getcockpit.com/u/Boerul)\
**Post date:** [February 11, 2020, 4:45pm UTC](https://discourse.getcockpit.com/t/file-asset-upload-trough-the-api/1208/1 "2020-02-11T16:45:45Z")

</div>

Is there any way that I can upload a file trough the API? Is there a endpoint for this scope?

---

<div class="post-metadata">

**Author:** ![artur](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/artur/32/4_2.png) [@artur](https://discourse.getcockpit.com/u/artur)\
**Post date:** [February 11, 2020, 8:16pm UTC](https://discourse.getcockpit.com/t/file-asset-upload-trough-the-api/1208/2 "2020-02-11T20:16:26Z")

</div>

take a look here [https://github.com/agentejo/cockpit/issues/763](https://github.com/agentejo/cockpit/issues/763)

---

<div class="post-metadata">

**Author:** ![Boerul](https://avatars.discourse-cdn.com/v4/letter/b/b5ac83/32.png) [@Boerul](https://discourse.getcockpit.com/u/Boerul)\
**Post date:** [February 12, 2020, 8:07am UTC](https://discourse.getcockpit.com/t/file-asset-upload-trough-the-api/1208/3 "2020-02-12T08:07:49Z")

</div>

Thank you! That’s useful. That being checked does anybody know how to send a file to the /api/collections/save/{collectionname} endpoint? I want to create an entry to a collection that contains a asset field. Can I send it as JSON to the endpoint or as form data? An example would be great.

 ![Capture](https://canada1.discourse-cdn.com/flex030/uploads/getcockpit/original/1X/d3ba3b41af2f16bc4ec64537b710fdaa76f1ed99.png)

---

<div class="post-metadata">

**Author:** ![raffaelj](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/raffaelj/32/331_2.png) [@raffaelj](https://discourse.getcockpit.com/u/raffaelj)\
**Post date:** [February 15, 2020, 6:09pm UTC](https://discourse.getcockpit.com/t/file-asset-upload-trough-the-api/1208/4 "2020-02-15T18:09:24Z")

</div>

That’s not possible. Maybe you could write some custom api endpoint, that checks for form data, than saves the asset and than adds the asset meta data to the entry and saves it.

You can split it in two requests. Send your asset. The meta data should be in the response. Than send your entry with the assets meta.

---

<div class="post-metadata">

**Author:** ![Boerul](https://avatars.discourse-cdn.com/v4/letter/b/b5ac83/32.png) [@Boerul](https://discourse.getcockpit.com/u/Boerul)\
**Post date:** [November 21, 2020, 10:16am UTC](https://discourse.getcockpit.com/t/file-asset-upload-trough-the-api/1208/5 "2020-11-21T10:16:19Z")

</div>

Hi again,  
I still can’t make this work. I’m trying to upload an asset using PHP cURL. Everything goes well when i test the process in postman but not in PHP (even if I copy and paste the code from postman).

 ![tempsnip](https://canada1.discourse-cdn.com/flex030/uploads/getcockpit/original/1X/82eb25b5dc855d033add4d4658543e462216082d.png)  
 ![tempsnip2](https://canada1.discourse-cdn.com/flex030/uploads/getcockpit/original/1X/d802cb4e2ed325d30d5bc579d57116c253bbb4b3.png)  
When I run the php code above I get no response from the API. If I specify the file name without ‘new CURLFILE()’ i get this response: {“uploaded”:[],“failed”:[],“assets”:[]}  
I tried a lot of approaches in the past week but with no luck.  
Do you have any idea what I’m doing wrong ?  
Do you have a working PHP example for uploading an asset to addAssets endpoint?

Thank you all!

---

<div class="post-metadata">

**Author:** ![ronaldaug](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/ronaldaug/32/260_2.png) [@ronaldaug](https://discourse.getcockpit.com/u/ronaldaug)\
**Post date:** [November 25, 2020, 3:30pm UTC](https://discourse.getcockpit.com/t/file-asset-upload-trough-the-api/1208/6 "2020-11-25T15:30:12Z")

</div>

> [@Boerul](#):
>
> I want to create an entry to a collection that contains a asset field

I think you don’t have to do it after you uploaded the file, it returns the whole path of the file. So, you’ll only have to add a new field like `file_link` to attach your new entry.

> [@Boerul](#):
>
> I still can’t make this work. I’m trying to upload an asset using PHP cURL. Everything goes well when i test the process in postman but not in PHP (even if I copy and paste the code from postman)

You are in the wrong HEADER format. As you’re sending file, you don’t have to add “Content-Type: application/json”

I also had the same issue before, actually it should be `files[]`, instead of `files[0]`.

This below example work 100%. Hope it helps you.

> **Upload via Javascript (AJAX)**
>
> **HTML**
> 
> ```html
> <input type="file" name="myFile" id="myFile">
> <button class="upload">Upload</button><br>
> <img class="preview-img">
> 
> ```
> 
> **Javascript**
> 
> ```javascript
> 
> const API_URL = '';
> const TOKEN = '';
> 
> function uploadImage(){
>   
> const PATH = '/api/cockpit/addAssets?token=';
> const api = API+PATH+TOKEN;
> const fileInput = document.querySelector('input[type="file"]');
> const formData = new FormData();
>   
> formData.append('files[]', fileInput.files[0]);
>   
>           
> fetch(url,{
> method : 'POST',
> body : formData
> })
> .then(e=>e.json())
> .then(res=>{
> const {assets} = res;
> if(!assets){
> alert('Upload failed!');
> return;
> }
>               
> const image_url = API_URL+'/storage/uploads'+assets[0].path;
>               
> document.querySelector(".preview-img").src= image_url;
>               
> alert('Successfully uploaded!');
> 
> })
> }
> 
> document.querySelector(".upload").addEventListener('click',()=>{
> uploadImage()
> })
> 
> ```

* * *

> **Upload via PHP ( CURL )**
>
> **PHP**
> 
> ```php
> <?php
> 
> function uploadImage($api,$token,$file){
> 
> $path = '/api/cockpit/addAssets?token=';
> $url = $api.$path.$token;
> $cfile = new CURLFile(realpath($file));
> $post = ['files[]' => $cfile];
> 
> $ch = curl_init();
> curl_setopt($ch, CURLOPT_URL, $url);
> curl_setopt($ch, CURLOPT_POST, 1);
> curl_setopt($ch, CURLOPT_HTTPHEADER,array('Content-Type: multipart/form-data'));
> curl_setopt($ch, CURLOPT_TIMEOUT, 10000);
> curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
> 
> $result = curl_exec ($ch);
> 
> return $result;
> };
> 
> $API = 'https://your_cockpit_api.com';
> $TOKEN = '80b0e4625d1e7ff753f1d45967a92f';
> $FILE = 'file.txt';
> $data = uploadImage($API,$TOKEN,$FILE);
> echo $data;
> 
> ```
