0

In WIX Backend, I'm struggling to find a way to upload a file to Dropbox using JS, fetch, etc.... I'm getting the following error. In addition where I need to mention the file I want to upload to Dropbox?

invalid json response body at https://content.dropboxapi.com/2/files/upload reason: Unexpected token E in JSON at position 0"

import { fetch } from 'wix-fetch';

export function xdrop() {
  const url = "https://content.dropboxapi.com/2/files/upload";

  const headers = {
    "Authorization": "Bearer " + 'here I wrote of course my token code',
    "Content-Type": "application/json",
    "Dropbox-API-Arg": JSON.stringify({
      "path": "/accwix.csv",
      "mode": {
        ".tag": "add"
      },
      "autorename": true,
      "mute": false
    })
  }

  const request = {
    "headers": headers,
  }

  console.log("request==>", request)

  return fetch(url, request)
    .then(response => response.json());
}
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
  • The "E" looks like "Exception" (or "Error") to me. You are not getting back JSON, you are getting plain-text, since the fetch failed. – Mr. Polywhirl Sep 24 '20 at 13:23
  • Hello @Shimon Suissa. Could you take a look to what the Dropbox API is returning and post it here? I'm pretty sure the problem is that you're not getting a valid JSON response from the API. – Ezequiel De Luca Sep 24 '20 at 13:25
  • Does this answer your question? ["SyntaxError: Unexpected token < in JSON at position 0"](https://stackoverflow.com/questions/37280274/syntaxerror-unexpected-token-in-json-at-position-0) – flaxel Sep 24 '20 at 13:25
  • Refer to the example for [fetch](https://www.wix.com/corvid/reference/wix-fetch/fetch) where you check `httpResponse.ok` and reject the promise if the response is not [200](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/200). – Mr. Polywhirl Sep 24 '20 at 13:28
  • you send 'Dropbox-API-Arg' as string and it's not JSON. check it by `console.log(headers)` – Ali U Sep 24 '20 at 13:33
  • Check your request response in "network" in your browsers dev tools – Mike Sep 24 '20 at 13:45
  • Thanks to all although it didn't help. I will create a new question since I installed DROPBOX npm in WIX, and I'm now getting Path not found error. Thanks anyway – Shimon Suissa Sep 26 '20 at 09:55

0 Answers0