cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
What’s new: end-to-end encryption, Replay and Dash updates. Find out more about these updates, new features and more 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: 

Bad HTTP "Content-Type" header

Bad HTTP "Content-Type" header

heisenberg41
New member | Level 2

Hello,

I am writing a simple app that creates some folders when we add a new client to our systems.

I keep getting an error:

Error in call to API function "files/create_folder_batch": Bad HTTP "Content-Type" header: "application/json; boundary=------------------------0e03e86aa71fa792". Expecting one of "application/json", "application/json; charset=utf-8", "text/plain; charset=dropbox-cors-hack".

Here is the code that I am using:

function create_folders() {
	$ch = curl_init();
	$folders = array(
	    "/B - Client Folders/" . $folder_name, 
	    "/B - Client Folders/" . $folder_name . "/Logo", 
	    "/B - Client Folders/" . $folder_name . "/Photos", 
	    "/B - Client Folders/" . $folder_name . "/Graphics"
	    );

	$params = array(
	    $folders, 
	    '"autorename":false', 
	    '"force_async":false');

	curl_setopt($ch, CURLOPT_URL, 'https://api.dropboxapi.com/2/files/create_folder_batch');
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
	
	curl_setopt($ch, CURLOPT_HTTPHEADER, array(
	    'Authorization: Bearer <key>',
	    'Dropbox-Api-Path-Root: {".tag": "namespace_id", "namespace_id": "<nsid>"}',
	    'Content-Type: application/json'));

	$result = curl_exec($ch);
	if (curl_errno($ch)) {
		echo 'Error:' . curl_error($ch);
	} else {echo $result;}
	curl_close($ch);
}

I don't understand why "; boundary=------------------------0e03e86aa71fa792" is being added to "application/json".

Thanks for your help in advance.

7 Replies 7

Greg-DB
Dropbox Staff

It looks like you're just passing along a native object as the API call parameters, and curl is then applying the multi-part Content-Type. You should send up the parameters as JSON.

You can build your parameters as JSON like:

$params = json_encode(array(
    'paths'=> $folders,
    'autorename'=> false,
    'force_async'=> false));

heisenberg41
New member | Level 2

Thank you Greg! That worked.  

Biggest noob question of all.  How did you know it had to be JSON encoded?

Greg-DB
Dropbox Staff

Great, thanks for confirming that.

All Dropbox API v2 endpoints (except OAuth endpoints) expect the parameters as JSON, though there are some different ways of sending them. You can find information about each one in the documentation. For example, the /2/files/create_folder_batch endpoint uses the "RPC" format, so it expects "JSON in the request body and return results as JSON in the response body".

eranga119
New member | Level 2

Error in call to API function "sharing/create_shared_link_with_settings": Bad HTTP "Content-Type" header: "application/json{\"path\":\"\\/1uawro.jpg\",\"settings\":{\"requested_visibility\":\"public\",\"audience\":\"public\",\"access\":\"viewer\"}}". Expecting one of "application/json", "application/json; charset=utf-8", "text/plain; charset=dropbox-cors-hack".

 

$share_link = array("path"=> $path, "settings"=> array("requested_visibility"=> "public", "audience"=> "public", "access"=> "viewer"));
$share_link_url = "https://api.dropboxapi.com/2/sharing/create_shared_link_with_settings";
$headers = array('Authorization: Bearer '. $token,
'Content-Type: application/json'.
json_encode($share_link)
);

 

i was encoded but not work for me.plz advice

Здравко
Legendary | Level 20

Hi @eranga119,

Why are you passing the body part (in your post: $share_link) in header part? The particular error message denote the sticking the body to the "Content-Type" header, which is NOT correct of course. 😉

Hope this helps.

Greg-DB
Dropbox Staff

@eranga119 This error message indicates that instead of just sending the expected "application/json" value in your "Content-Type" header, you're instead sending that plus the JSON string for the API call parameters, as Здравко said. That JSON should be sent in the request body, not a header.

 

The code you shared does seem to show you are incorrectly appending those parameters to your "Content-Type" header value, so you should fix your code to put that in the request body instead.

eranga119
New member | Level 2

@greg-DB thank you for quick responce.i was fix my code and it's work perfectly now. you are abslutly correct.

Need more support?
Who's talking

Top contributors to this post

  • User avatar
    eranga119 New member | Level 2
  • User avatar
    Greg-DB Dropbox Staff
  • User avatar
    Здравко Legendary | Level 20
What do Dropbox user levels mean?