【问题标题】:Signed upload to cloudinary with node-fetch instead of request使用 node-fetch 而不是 request 签名上传到 cloudinary
【发布时间】:2020-05-19 13:15:37
【问题描述】:

我能够使用节点request module 通过 REST API 上传到 Cloudinary,如下所示:

 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
  })
 })

我只使用云上传请求,否则我在整个应用程序中使用 node-fetch。我想使用 node-fetch 进行 cloudinary 上传,但应用与上面工作示例中相同的逻辑会导致神秘的错误消息:'Upload preset must be specified when using unsigned...'

我的节点获取请求如下所示:

 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
 })

【问题讨论】:

标签: node.js post request fetch cloudinary


【解决方案1】:

您是否考虑过使用我们的 Node SDK 来实现这一目标? 请查看文档以获取更多信息:https://cloudinary.com/documentation/node_integration

【讨论】:

  • 是的。我现在正在这样做。工作一种享受。一直是 cloudinary 的早期采用者,刚开始的时候还没有 node SDK。
【解决方案2】:

我知道这是一篇旧帖子,但您的获取正文需要在 JSON.stringify({file,api_key, ...}) 中

我现在就是这样用的,谁需要一个额外的包: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
  })
 })

【讨论】:

    猜你喜欢
    • 2019-05-25
    • 2023-03-16
    • 2017-05-10
    • 2022-12-16
    • 1970-01-01
    • 2016-12-19
    • 2019-12-14
    • 2015-11-01
    • 2019-01-09
    相关资源
    最近更新 更多