5

I need to send post request with custom parameter("data" containing path) and set content type as text/plain. I looked through a ton of similar question but none of the solutions posted helped.

The method should list files from this directory.

my code is

    public List<FileWrapper> getFileList() {

    MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
    map.add("data", "/public/");

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.TEXT_PLAIN);

    HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(
            map, headers);
    String url = "http://192.168.1.51:8080/pi/FilesServlet";
    restTemplate.getMessageConverters().add(new FormHttpMessageConverter());
    restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
    String response = restTemplate
            .postForObject(url, request, String.class);
    List<FileWrapper> list = new ArrayList<>();
    for (String part : response.split("\\|")) {
        System.out.println("part " + part);
        list.add(new FileWrapper(part));
    }
    return list;
}

Here's working code equivalent written in javascript:

function getFileList(direction){
$("div.file-list").html("<center><progress></progress></center>");
$.ajax({
  url: "http://192.168.1.51:8080/pi/FilesServlet",
  type: "POST",
  data: direction ,
  contentType: "text/plain"
})

The parameter is not added as the request returns empty string meaning the path is not valid. The expected response is file_name*file_size|file_name*file_size ...

Thanks in advance.

10
  • UrlEncode the $data field value and append it at its place in the url String Commented Feb 5, 2015 at 15:25
  • @RobertRowntree data is not a part of the url, this is POST method, data value is an argument.
    – Asalas77
    Commented Feb 5, 2015 at 15:48
  • then write it to the output stream on the Connection or put it into the 'Entity' depending on what client is being used. Commented Feb 5, 2015 at 15:52
  • @RobertRowntree The thing is I want to avoid using HttpUrlConnection and use Spring's RestTemplate instead to handle connections
    – Asalas77
    Commented Feb 5, 2015 at 16:43
  • @Asalas77 is the input data in AJAX request in this format? {'data':'/public/'} Can you print direction in JS code and post that back here?
    – nilesh
    Commented Feb 5, 2015 at 16:45

1 Answer 1

12

From the discussion in the comments, it's quite clear that your request object isn't correct. If you are passing a plain string containing folder name, then you don't need a MultiValueMap. Just try sending a string,

    String data = "/public/"
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.TEXT_PLAIN);

    HttpEntity<String> request = new HttpEntity<String>(
            data, headers);
    String url = "http://192.168.1.51:8080/pi/FilesServlet";
    restTemplate.getMessageConverters().add(new FormHttpMessageConverter());
    restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
    String response = restTemplate
            .postForObject(url, request, String.class);
2
  • Could you show an example how to send a file with this method as multipart form data?
    – Asalas77
    Commented Feb 9, 2015 at 10:29
  • Did you try content type header to MediaType.MULTIPART_FORM_DATA
    – nilesh
    Commented Feb 9, 2015 at 14:37

Not the answer you're looking for? Browse other questions tagged or ask your own question.