0

I am capable of uploading to the Cloudinary via the REST API using the Node request module like so:

 request.post({
  url: `https://api.cloudinary.com/v1_1/dennis/image/upload`,
  body: {
   file: `data:image/jpeg;base64,${req.body.toString('base64')}`,
   api_key: key,
   folder: 'dennis',
   timestamp: ts,
   signature: sig
  },
  json: true
 }, async (err, response, body) => {

  if (err) return console.error(err)

  res.send({
   'public_id': body.public_id,
   'secure_url': body.secure_url
  })
 })

I am only using the request for the cloudinary upload otherwise I use node-fetch throughout my application. I would like to use node-fetch for the cloudinary upload but applying the same logic as in the working example above results in the cryptic error message: 'Upload preset must be specified when using unsigned...'

My node fetch request looks like this:

 let response = await fetch(
  `https://api.cloudinary.com/v1_1/dennis/image/upload`,
  {
   method: 'post',
   body: {
    file: `data:image/jpeg;base64,${req.body.toString('base64')}`,
    api_key: key,
    folder: 'dennis',
    timestamp: ts,
    signature: sig
  }
 })

 if (response.status === 400) return console.error(response)

 let body = await response.json()

 res.send({
  'public_id': body.public_id,
  'secure_url': body.secure_url
 })

Dennis Bauszus
  • 1,624
  • 2
  • 19
  • 44
  • Does this answer your question? [Cloudinary image upload from React: am including Cloudinary unsigned preset but get "Upload preset must be specified when using unsigned upload"](https://stackoverflow.com/questions/51633061/cloudinary-image-upload-from-react-am-including-cloudinary-unsigned-preset-but) – technophyle Feb 03 '20 at 18:42
  • @technophyle I am using a signed upload though, not unsigned. – Dennis Bauszus Feb 14 '20 at 16:11

2 Answers2

1

Have you consider using our Node SDK to achieve this? Please check the documentation for more info: https://cloudinary.com/documentation/node_integration

Leptians
  • 96
  • 4
  • Yeah. I am doing this now. Works a treat. Have been a cloudinary early adopter and there wasn't a node SDK when I got started. – Dennis Bauszus Feb 22 '20 at 15:47
1

I know this is an old post, but your fetch body needs to be in a JSON.stringify({file,api_key, ...})

I'm using it like that right now, who needs an extra package :P

let response = await fetch(
  URL,
  {
   method: 'POST',
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json'
    },
   body:JSON.stringify({
    file,
    api_key,
    timestamp,
    public_id: "temp/image",
    signature
  })
 })
Xigrux
  • 11
  • 1