We’re aware of an issue causing slower load times on the Dropbox Community forum. It should be resolved soon. Thanks for your patience! 

Forum Discussion

Abhay Yadav's avatar
Abhay Yadav
Explorer | Level 4
6 years ago

Bad JSON: expected object value. exception while uplaoding a file using dropbox Java SDK

Hi all,

 

I am trying to upload a file using dropbox Java SDK (version 3.1.5). I amgetting file from a ajax form and then trying to upload it but getting the below exception when calling uploadAndFinish() method.

 

com.dropbox.core.BadResponseException: Bad JSON: expected object value.
 at [Source: ; line: 1, column: 1]
	at com.dropbox.core.DbxRequestUtil.unexpectedStatus(DbxRequestUtil.java:347)
	at com.dropbox.core.DbxRequestUtil.unexpectedStatus(DbxRequestUtil.java:324)
	at com.dropbox.core.DbxUploader.finish(DbxUploader.java:268)
	at com.dropbox.core.DbxUploader.uploadAndFinish(DbxUploader.java:126)
	at com.dropbox.core.DbxUploader.uploadAndFinish(DbxUploader.java:96)
	at com.dropbox.core.v2.DbxUploadStyleBuilder.uploadAndFinish(DbxUploadStyleBuilder.java:92)
	at com.muletutorials.dropbox.DropboxService.uploadFile(DropboxService.java:81)
	at com.muletutorials.controller.MuletutorialsController.uploadArticle(MuletutorialsController.java:317)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:175)
	at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:446)
	at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:434)
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
	at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:316)
	at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:126)
	at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:90)
	at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
	at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:114)
	at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
	at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:122)
	at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
	at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111)
	at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
	at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:169)
	at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
	at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:48)
	at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
	at org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilterInternal(BasicAuthenticationFilter.java:213)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108)
	at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
	at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:64)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108)
	at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
	at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:53)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108)
	at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
	at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:91)
	at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
	at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:213)
	at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:176)
	at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:344)
	at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:261)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:543)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
	at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
	at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:615)
	at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
	at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:818)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1627)
	at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Thread.java:748)
Caused by: com.fasterxml.jackson.core.JsonParseException: expected object value.
 at [Source: ; line: 1, column: 1]
	at com.dropbox.core.stone.StoneSerializer.expectStartObject(StoneSerializer.java:91)
	at com.dropbox.core.ApiErrorResponse$Serializer.deserialize(ApiErrorResponse.java:53)
	at com.dropbox.core.ApiErrorResponse$Serializer.deserialize(ApiErrorResponse.java:36)
	at com.dropbox.core.stone.StoneSerializer.deserialize(StoneSerializer.java:55)
	at com.dropbox.core.DbxRequestUtil.unexpectedStatus(DbxRequestUtil.java:343)
	... 74 more

This is what I have tried yet

public boolean uploadFile(StringBuilder fileName, InputStream fileStream) throws DbxException {
		DbxClientV2 client = createClient();
		String path = "/" + fileName.toString();
		try {
			System.out.println("STARTING...............");
			UploadBuilder ub = client.files().uploadBuilder(path);
			System.out.println("ub done");
			ub.withMode(WriteMode.ADD);
			System.out.println("write mode done");
			FileMetadata metadata = ub.uploadAndFinish(fileStream);
			System.out.println("File Uploaded");
			System.out.println(metadata.toStringMultiline());
			System.out.println("FINISHED...............");
			return true;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return false;
	}

The code works till write Mode Done logger. It is failing at the next call, don't know what I am doing wrong.

 

Please help !!!

9 Replies

  • Abhay Yadav's avatar
    Abhay Yadav
    Explorer | Level 4
    6 years ago

    I debugged the flow and found the issue. When calling the uploadAndFinish(), somehwere down the line, the API was returning 401  status code, the stack trace was not much helpful though to get the root cause.

    I regenerated the access token and it worked!!!

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    6 years ago

    Thanks for following up. That actually sounds like a bug in the SDK then. The SDK should automatically convert the 'invalid_access_token' error response to a proper exception. I'll look into it and ask the team to fix that up.

  • FilipRachunek's avatar
    FilipRachunek
    Explorer | Level 4
    5 years ago

    Hello,

     

    I have the same problem, using Dropbox Java SDK 3.1.5. I suspect the issue could be related to new short-lived access tokens and scoped apps, as I don't see this error with "no expiration" token and "full Dropbox" permission type (created in the past).

     

    So, my first idea was that some scoped permissions were missing, as all read operations work fine, and the problem occurs only with "uploadAndFinish(inputStream)" method. However, I checked the permissions and I didn't see anything wrong there - there are checked checkboxes at files.metadata.write, files.metadata.read, files.content.write, files.content.read.

     

    Can you help, please? I am out of ideas.

     

    Filip

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    5 years ago

    FilipRachunek This is a bug in the Java SDK. This is still open with engineering, but I don't have an update on it yet. I'll follow up here once I do.

  • FilipRachunek's avatar
    FilipRachunek
    Explorer | Level 4
    5 years ago

    @Greg-DB Thanks for your prompt response. I'll wait for the SDK update.

     

    Filip

     

  • FilipRachunek's avatar
    FilipRachunek
    Explorer | Level 4
    5 years ago

    Hello,

     

    The error is still there, even in version 4.0.0 of the Java SDK.

    com.dropbox.core.BadResponseException: Bad JSON: expected object value.
     at [Source: (String)""; line: 1, column: 1]
    	at com.dropbox.core.DbxRequestUtil.unexpectedStatus(DbxRequestUtil.java:347) ~[dropbox-core-sdk-4.0.0.jar:na]
    	at com.dropbox.core.DbxRequestUtil.unexpectedStatus(DbxRequestUtil.java:324) ~[dropbox-core-sdk-4.0.0.jar:na]
    	at com.dropbox.core.DbxUploader.finish(DbxUploader.java:268) ~[dropbox-core-sdk-4.0.0.jar:na]
    	at com.dropbox.core.DbxUploader.uploadAndFinish(DbxUploader.java:126) ~[dropbox-core-sdk-4.0.0.jar:na]
    	at com.dropbox.core.DbxUploader.uploadAndFinish(DbxUploader.java:96) ~[dropbox-core-sdk-4.0.0.jar:na]
    	at com.dropbox.core.v2.DbxUploadStyleBuilder.uploadAndFinish(DbxUploadStyleBuilder.java:92) ~[dropbox-core-sdk-4.0.0.jar:na]

     Any chance to fix it soon? I understand that long-lived access tokens will stop working by the end of September 2021, so we'll have to switch to short-lived. However, it's not possible as long as the SDK throws this exception.

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    5 years ago

    FilipRachunek This issue is still open with engineering, but I don't have an update or timeline right now. I'll follow up here once I do.

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    5 years ago

    This is fixed in v4.0.1 of the Java SDK.

About Dropbox API Support & Feedback

Node avatar for Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.

The Dropbox Community team is active from Monday to Friday. We try to respond to you as soon as we can, usually within 2 hours.

If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X, Facebook or Instagram.

For more info on available support options for your Dropbox plan, see this article.

If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!