cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Want to learn some quick and useful tips to make your day easier? Check out how Calvin uses Replay to get feedback from other teams at Dropbox here.

Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

save_url throws error "request body: could not decode input as JSON"

save_url throws error "request body: could not decode input as JSON"

sr_c
Explorer | Level 3
Go to solution

Hi

I am trying to upload files (pictures with .jpg extension) to my dropbox account (to a specific app folder). I am using Airtable scripting (Javascript) extension to upload files from Airtable  (which has these pictures as attachments)

As these attachments are in Airtable (available at individual URLs), I am using below dropbox URL

When I use Dropbox API explorer it is able save the file to the dropbox folder.
But when I use through Airtable scripting block I am getting below error,
"Error in call to API function "files/save_url": request body: could not decode input as JSON"
main part of the script attached below
--------------------------------------------------------
// set the endpoint and app token
let dropboxEndpoint = "https://api.dropboxapi.com/2/files/save_url";
let appToken = "<--my dropbox app token -->"
// set up the post options
let postOptions = {
method: "POST",
headers: {
"Authorization" : "Bearer " + appToken,
"Content-Type" : "application/json"
},
data: {
"path":"/myairsri/best_peacock.jpg",
"url":"<-- Airtable attachmet URL -->"
}
}
const postResults = await fetch(dropboxEndpoint, postOptions);
const jsonPost = await postResults.text();
console.log(jsonPost)
----------------------------------------------------
Please suggest how this can be resolved.
 
Thanks
Sr
1 Accepted Solution

Accepted Solutions

Здравко
Legendary | Level 20
Go to solution

@sr_c wrote:
...
},
data: filedata
}
...

@sr_c, When you are preparing a request body with appropriate data, you have to set those data. What actually is going on here? 🤔 You have already prepared request body (that you named as 'filedata' - incorrect name and probably confusing)! In this context instead of setting 'data', you have to set 'body'. 😉 Be more careful!

Hope this helps.

View solution in original post

7 Replies 7

Здравко
Legendary | Level 20
Go to solution

@sr_c wrote:
...
"Error in call to API function "files/save_url": request body: could not decode input as JSON"
main part of the script attached below
--------------------------------------------------------
...
let postOptions = {
method: "POST",
headers: {
"Authorization" : "Bearer " + appToken,
"Content-Type" : "application/json"
},
data: {
"path":"/myairsri/best_peacock.jpg",
"url":"<-- Airtable attachmet URL -->"
}
}
const postResults = await fetch(dropboxEndpoint, postOptions);
...

Hi @sr_c,

You have correctly set json type for the request payload, but where you're ensuring passed data in the body is correct JSON format? 🤔 Maybe passing something like processed body with 'JSON.stringify' can solve your issue. 😉

By the way, you can use Dropbox SDK for Javascript to do the same.

Good luck.

Greg-DB
Dropbox Staff
Go to solution

@sr_c As Здравко said, you'll need to make sure you're sending a JSON string. It doesn't look like you're doing so in your current code. We do recommend using the official Dropbox JavaScript SDK though, as it will do much of the work for you.

sr_c
Explorer | Level 3
Go to solution

Hi

Based on the suggestion to use JSON.stringify, I tried to implement below solution but error remains same. Not sure whether this is the right way to implement the suggestion. Changes highlighted in blue

------------------------------------------------------------------------------------------

 
let appToken = "<<App Token>>"
let fileobj = {
                    "path":"/myairsri/best_peacock.jpg",
                   "url":urlval
                 }
let filedata = JSON.stringify(fileobj)
// set up the post options
let postOptions = {
method: "POST",
headers: {
"Authorization" : "Bearer " + appToken,
"Content-Type" : "application/json"
},
data: filedata
}
Please let me know your suggestions
Thanks

Здравко
Legendary | Level 20
Go to solution

@sr_c wrote:
...
},
data: filedata
}
...

@sr_c, When you are preparing a request body with appropriate data, you have to set those data. What actually is going on here? 🤔 You have already prepared request body (that you named as 'filedata' - incorrect name and probably confusing)! In this context instead of setting 'data', you have to set 'body'. 😉 Be more careful!

Hope this helps.

Greg-DB
Dropbox Staff
Go to solution

@sr_c I see you're using fetch to perform the call, so make sure you're using the right options to construct the call as needed. As Здравко said, fetch expects a parameter named 'body' to set the request body, not 'data'.

sr_c
Explorer | Level 3
Go to solution

Hello  Здравко

Your suggestion worked like a charm and I am able to upload files. Thanks a lot

But I am wondering why Dropbox API documentation is showing 'data' instead of 'body' in the 'Example' (screen shot attached below)

dropbox_save_url.png

 

Здравко
Legendary | Level 20
Go to solution

The example on the page, you posted, uses 'curl' shell command. That's the way (one of all actually) body to be represented. You're working with 'fetch' command in javascript, not shell! Just different things! 😉 Pay attention on the proper documentation. None of these parameters (how they are named) can be seen by Dropbox server - just properly formated request is expected.

Need more support?
Who's talking

Top contributors to this post

  • User avatar
    Здравко Legendary | Level 20
  • User avatar
    sr_c Explorer | Level 3
  • User avatar
    Greg-DB Dropbox Staff
What do Dropbox user levels mean?