# 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:** 1\
**Showing post:** 6

<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;
> 
> ```

---

_[View the full topic](https://discourse.getcockpit.com/t/file-asset-upload-trough-the-api/1208)._
