admin管理员组

文章数量:1253980

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.

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.

Share Improve this question edited Feb 5, 2015 at 18:08 Asalas77 asked Feb 5, 2015 at 14:15 Asalas77Asalas77 6324 gold badges15 silver badges26 bronze badges 10
  • UrlEncode the $data field value and append it at its place in the url String – Robert Rowntree 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. – Robert Rowntree 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
 |  Show 5 more ments

1 Answer 1

Reset to default 12

From the discussion in the ments, 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);

本文标签: javaHow to send POST request through RestTemplate with custom parameter in headerStack Overflow