We are aware of the issue with the badge emails resending to everyone, we apologise for the inconvenience - learn more here.

Forum Discussion

heisenberg41's avatar
heisenberg41
New member | Level 2
5 years ago

Bad HTTP "Content-Type" header

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.

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Staff rankDropbox 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's avatar
      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's avatar
        Greg-DB
        Icon for Dropbox Staff rankDropbox 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's avatar
      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

      • Здравко's avatar
        Здравко
        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.