How to make direct api call to cloudinary with axios?

Viewed 4387

I am trying to figure this out with axios. I have made the direct api call with superagent and now want to know how to use with axios as the rest of my project is with axios. I know there is cloudinary-react, but this is the way I prefer to do it. Here is what I have so far.

import React, { Component } from 'react';
import Dropzone from 'react-dropzone';
import sha1 from 'sha1';
import superagent from 'superagent';
import axios from 'axios';

class Images extends Component {
  uploadFile(files) {
    console.log('uploadFile: ');
    const image = files[0];

    const cloudName = 'tbaustin';
    const url = `https://api.cloudinary.com/v1_1/${cloudName}/image/upload`;

    const timestamp = Date.now()/1000;
    const uploadPreset = 'cnh7rzwp';

    const paramsStr = `timestamp=${timestamp}&upload_preset=${uploadPreset}secret`;

    const signature = sha1(paramsStr);
    const params = {
      'api_key': 'api_key',
      'timestamp': timestamp,
      'upload_preset': uploadPreset,
      'signature': signature
    }

    let uploadRequest = superagent.post(url)
    uploadRequest.attach('file', image);

    Object.keys(params).forEach((key) => {
      uploadRequest.field(key, params[key]);
    });

    uploadRequest.end((err, res) => {
      if(err) {
        alert(err);
        return
      }

      console.log('UPLOAD COMLETE: '+JSON.stringify(res.body));
    });



//AXIOS CONTENT HERE
    // let request = axios.post(url, {file: image});
    // request.then((response) => {
    //   Object.keys(params).forEach((key) => {
    //     uploadRequest.field(key, params[key]);
    //   });
    //   console.log('UPLOAD COMPLETE: '+JSON.stringify(response.body));
    // }).catch((err) => { alert(err); });
  }

  render() {
    return (
      <div>
        <Dropzone onDrop={this.uploadFile.bind(this)}/>
      </div>
    )
  }
}

export default Images;
2 Answers

This worked for me.

    let formData = new FormData();
    formData.append("api_key",'');
    formData.append("file", image);
    formData.append("public_id", "sample_image");
    formData.append("timestamp", timeStamp);
    formData.append("upload_preset", uploadPreset);

    axios
    .post(url, formData)
    .then((result) => {
        console.log(result);
    })
    .catch((err) => {
        console.log(err);
    })    

This is a part of my project when I tried to upload an avatar. I have set my upload preset to unsigned on cloudinary.

    const uploadPhotoHandler = async (e) => {
         const file = e.target.files[0];
         const formData = new FormData();
         formData.append("file", file);
         formData.append("upload_preset", "pa*****");

         try {
               setPictureProfile("./assets/img/giphy.gif");
               const res = await axios.post("https://api.cloudinary.com/v1_1/${cloud_name}/upload", formData);
               console.log(res.data.url);
               setPictureProfile(res.data.url);
         } catch (error) {}
    };
Related