I am trying to send a POST request on web server with Android Annotations REST Api, and I am trying to send an image as a Base64 String and video as a file. The operation also needs a Bearer Auth but I have managed that without an error.
My RestClient interface is like this:
@Rest(rootUrl = NetworkConstants.BASE_URL, converters = {GsonHttpMessageConverter.class, StringHttpMessageConverter.class,
FormHttpMessageConverter.class},interceptors = {
HeadersRequestInterceptor.class })
public interface RestClient extends RestClientHeaders {
@Post(NetworkConstants.UPLOAD_URL)
VideoUploadResponse uploadVideo(MultiValueMap<String,Object> model);
}
And my Request is like this:
MultiValueMap dataMultiPart = new LinkedMultiValueMap<>();
File file = new File(path);
dataMultiPart.add("thumbnail", bitmapString);
dataMultiPart.add("media", new FileSystemResource(file));
try {
VideoUploadResponse resonse = restClient.uploadVideo(dataMultiPart);
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
In my interceptor class I have tried both this
public class HeadersRequestInterceptor implements ClientHttpRequestInterceptor {
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
request.getHeaders().add("content-type", MediaType.MULTIPART_FORM_DATA_VALUE);
request.getHeaders().add("authorization", "Bearer sometokenvalue");
return execution.execute(request, body);
}
}
and this
public class HeadersRequestInterceptor implements ClientHttpRequestInterceptor {
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
request.getHeaders().setContentType(MediaType.MULTIPART_FORM_DATA);
request.getHeaders().add("authorization", "Bearer sometokenvalue");
return execution.execute(request, body);
}
}
And I am getting the exception Invalid token character ' ' in token "json charset=UTF-8" everytime.
At first I thought there might be a problem with the auth token, but there is not.And when I try this request with postman there is no such error.
What am I doing wrong?
This can be achieved in several ways:
@Rest(rootUrl = rootUrl = NetworkConstants.BASE_UR, converters = FormHttpMessageConverter.class, interceptors = HeadersRequestInterceptor.class)
public interface RestClient extends RestClientHeaders {
@Post(NetworkConstants.UPLOAD_URL)
@RequiresHeader(HttpHeaders.CONTENT_TYPE)
VideoUploadResponse uploadVideo(@Path int id, @Body MultiValueMap<String, Object> data);
}
MultiValueMap dataMultiPart = new LinkedMultiValueMap<>();
File file = new File(path);
dataMultiPart.add("thumbnail", bitmapString);
dataMultiPart.add("media", new FileSystemResource(file));
try {
restClient.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.MULTIPART_FORM_DATA_VALUE);
VideoUploadResponse resonse = restClient.uploadVideo(dataMultiPart);
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
It is even easier in AA 4.0:
@Rest(rootUrl = rootUrl = NetworkConstants.BASE_UR, converters = FormHttpMessageConverter.class, interceptors = HeadersRequestInterceptor.class)
public interface RestClient extends RestClientHeaders {
@Post(NetworkConstants.UPLOAD_URL)
VideoUploadResponse uploadVideo(@Part String thumbnail, @Part FileSystemResource media);
}
restClient.uploadVideo(bitmapString, new FileSystemResource(file));
So it turns out problem was not because of me, but the server side.
Server is sending "application/json charset=UTF-8" as Content-Type, but Spring framework is strict in parsing headers, and it sees the space character between "json" and "charset" as an error.Normally it should be "application","json;charset=utf-8”;
That's why problem was happening while using Android Annotations or Retrofit.
It was working well with Postman because it is less strict in parsing headers.
I am using now Google Volley for network communication and the problem is gone.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.