I am using rest api to send file with some data. Below is the signature of API
@RequestMapping(value = "/file", method = RequestMethod.POST)
public ModelAndView uploadFile(HttpServletRequest request,
HttpServletResponse response,
@RequestParam(required = false) String wfid,
@RequestParam String ssoToken,
@RequestParam(required = false) String typeMedia,
@RequestParam(required = false) String synopsisParam,
@RequestParam(required = false) String slideShowParam,
@RequestParam(required = false) String embedInContentParam,
@RequestParam(required = false) boolean sizeRestrictionRequiredFlag,
@RequestParam MultipartFile file) throws Exception {
logger.info("SSO - " + ssoToken);
return "Output";
}
Below is my method in which i am creating POST request
void myMethod(String restAPI, String ssoId, byte[] imageByte){
PostMethod post = null;
HttpClient httpClient = new HttpClient();
try {
post = new PostMethod(restAPI);
Part[] parts = new Part[] { new FilePart("myImage.JPG", new ByteArrayPartSource("myImage.JPG", imageByte))};
HttpMethodParams par = post.getParams();
par.getDefaults().setParameter("ssoToken", ssoId);
MultipartRequestEntity multipartRequestEntity = new MultipartRequestEntity(parts, post.getParams());
ByteArrayOutputStream requestContent = new ByteArrayOutputStream();
multipartRequestEntity.writeRequest(requestContent);
post.setRequestEntity(multipartRequestEntity);
post.setRequestHeader("content-type", multipartRequestEntity.getContentType());
/*NameValuePair[] postParameters = new NameValuePair[]{new NameValuePair("ssoToken", ssoId)};
post.setRequestBody(postParameters);*/
int status = httpClient.executeMethod(post);
String responseBody = post.getResponseBodyAsString();
} catch(Exception e){
} finally{
if(post != null){
post.releaseConnection();
}
}
}
I am getting 400 - Required String parameter 'ssoToken' is not present
When i tried commented code /NameValuePair[] postParameters = new NameValuePair[]{new NameValuePair("ssoToken", ssoId)}; post.setRequestBody(postParameters);/
in above method and send NameValue pair as response body above exception is resolve but Multipart file exception occur.
Can someone help how create request to rest controller mentioned above. Kindly let me know the issue before down vote.
Thanks in advance