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: 

Dropbox API problem

Dropbox API problem

devolajide
New member | Level 2

I am developing an ap using Flutter framework. My app involves me using dropbox api to upload folders from my phone to my dropbox account. The problem I am having now is an error with content-type in the API. What content-type can I use for dropbox in my flutter app?

 

Thanks

4 Replies 4

Greg-DB
Dropbox Staff

[Cross-linking for reference: https://stackoverflow.com/questions/57692602/flutter-http-content-type ]

For reference, can you share the relevant code, and the error you're getting?

When uploading to Dropbox via the API, e.g., using the /2/files/upload, the Dropbox API expects the file data in the request body, with a "Content-Type" of "application/octet-stream". 

h21
Explorer | Level 4

Here's the code

 String token = API().token;
      var map = new Map<String, String>();
      map["Dropbox-API-Arg"] = '{"path": "/EasyBay/Project1/image.txt"}';

      Map<String, String> requestHeaders = {
        'Content-type': 'application/octet-stream', 
        'User-Agent': 'api-explorer-client',
       // 'Dropbox-API-Arg': '{"path": "/EasyBay/Project1/image.txt"}',
        'Authorization': 'Bearer' + token
      };

      var response = await http.post(new API().upload_link, body: map, headers: requestHeaders);
      print(response.statusCode);
      return parseChat(response.body);

While this is the error

Spoiler
E/flutter ( 4498): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: Bad state: Cannot set the body fields of a Request with content-type "application/octet-stream".
E/flutter ( 4498): #0 Request.bodyFields= (package:http/src/request.dart:124:7)
E/flutter ( 4498): #1 BaseClient._sendUnstreamed (package:http/src/base_client.dart:163:17)
E/flutter ( 4498): <asynchronous suspension>
E/flutter ( 4498): #2 BaseClient.post (package:http/src/base_client.dart:54:7)
E/flutter ( 4498): #3 post.<anonymous closure> (package:http/http.dart:70:16)
E/flutter ( 4498): #4 _withClient (package:http/http.dart:166:20)
E/flutter ( 4498): <asynchronous suspension>
E/flutter ( 4498): #5 post (package:http/http.dart:69:5)
E/flutter ( 4498): #6 uploadFolder (package:eastbay/src/Api/uploadApi.dart:25:28)
E/flutter ( 4498): <asynchronous suspension>
E/flutter ( 4498): #7 _UploadFolderState.Upload (package:eastbay/src/ui/upload.dart:43:19)
E/flutter ( 4498): #8 _UploadFolderState.build.<anonymous closure> (package:eastbay/src/ui/upload.dart:28:15)
E/flutter ( 4498): #9 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:654:14)
E/flutter ( 4498): #10 _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:729:32)
E/flutter ( 4498): #11 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:182:24)
E/flutter ( 4498): #12 TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:365:11)
E/flutter ( 4498): #13 TapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:275:7)
E/flutter ( 4498): #14 PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:455:9)
E/flutter ( 4498): #15 PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:75:13)
E/flutter ( 4498): #16 PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:102:11)
E/flutter ( 4498): #17 GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:218:19)
E/flutter ( 4498): #18 GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:198:22)
E/flutter ( 4498): #19 GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:156:7)
E/flutter ( 4498): #20 GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:102:7)
E/flutter ( 4498): #21 GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:86:7)
E/flutter ( 4498): #22 _rootRunUnary (dart:async/zone.dart:1136:13)
E/flutter ( 4498): #23 _CustomZone.runUnary (dart:async/zone.dart:1029:19)
E/flutter ( 4498): #24 _CustomZone.runUnaryGuarded (dart:async/zone.dart:931:7)
E/flutter ( 4498): #25 _invoke1 (dart:ui/hooks.dart:263:10)
E/flutter ( 4498): #26 _dispatchPointerDataPacket (dart:ui/hooks.dart:172:5)
E/flutter ( 4498):

Greg-DB
Dropbox Staff

Thanks! It looks like the error message is:

"Bad state: Cannot set the body fields of a Request with content-type "application/octet-stream"."

That's not an error message from the Dropbox API itself. It appears to be an error message from the HTTP client you're using. It looks like the problem is that you're attempting to set the "body" parameter to be a Map value, which isn't supported in this case.

When uploading to Dropbox via an upload API endpoint, the request body should contain the file data. In your code you're attempting to set it to a Map contain the API parameters, which wouldn't work anyway.

You should update your code to send the file data in the request body, and set the API parameters for the API call in a "Dropbox-API-Arg" request header, which would be in the "requestHeaders" variable you currently have (like the commented out line).

h21
Explorer | Level 4

Thanks. I will check it out

Need more support?
Who's talking

Top contributors to this post

  • User avatar
    h21 Explorer | Level 4
  • User avatar
    Greg-DB Dropbox Staff
What do Dropbox user levels mean?