1

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

1 Answer 1

1

The reason this is not working, is because, the Name-Value in parameters do not isolate customer Request Parameters that your Rest api needs.

Essentially, java wouldn't know where to look into, to extract the RequestParam "ssoToken" that your rest controller expects.

What you can do, however is this -

Part[] parts  = new Part[] { new FilePart("file",file), new StringPart("ssoToken",ssoToken) };
MultipartRequestEntity multipartRequestEntity = new MultipartRequestEntity(parts, par);
HttpMethodParams par = post.getParams();

Then proceed with the rest of the code as is.

The controller isolates the parts and searches for the "ssoToken" object, which it will now find!

This, I beleive should fix this problem!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.