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: 

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

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

Abhay Yadav
Explorer | Level 4

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 9

Greg-DB
Dropbox Staff

[Cross-linking for reference: https://stackoverflow.com/questions/63973139/bad-json-expected-object-value-dropbox-api-file-upload-... ]

 

Thanks for the report! This error doesn't reproduce for me with this code though, so we'll need to investigate more. Does this error occur for you when uploading to any file path, or just some path(s) in particular?

Abhay Yadav
Explorer | Level 4

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
Dropbox Staff

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
Explorer | Level 4

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
Dropbox Staff

@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
Explorer | Level 4

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

 

Filip

 

FilipRachunek
Explorer | Level 4

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
Dropbox Staff

@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
Dropbox Staff

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

Need more support?