Spring RestTemplate POST Request with URL encoded data

后端 未结 3 849
忘了有多久
忘了有多久 2021-02-18 22:18

I\'m new to Spring and trying to do a rest request with RestTemplate. The Java code should do the same as below curl command:

curl --data \"name=feature&colo         


        
相关标签:
3条回答
  • 2021-02-18 22:40

    It may be a Header issue check if the header is a Valid header, are u referring to "BasicAuth" header?

    HttpHeaders headers = new HttpHeaders();
    headers.add("Content-Type", MediaType.APPLICATION_FORM_URLENCODED.toString());
    headers.add("Accept", MediaType.APPLICATION_JSON.toString()); //Optional in case server sends back JSON data
        
    MultiValueMap<String, String> requestBody = new LinkedMultiValueMap<String, String>();
    requestBody.add("name", "feature");
    requestBody.add("color", "#5843AD");
        
    HttpEntity formEntity = new HttpEntity<MultiValueMap<String, String>>(requestBody, headers);
        
    ResponseEntity<LabelCreationResponse> response = 
       restTemplate.exchange("https://example.com/api/request", HttpMethod.POST, formEntity, LabelCreationResponse.class);
    
    0 讨论(0)
  • 2021-02-18 22:41

    I think the problem is that when you try to send data to server didn't set the content type header which should be one of the two: "application/json" or "application/x-www-form-urlencoded" . In your case is: "application/x-www-form-urlencoded" based on your sample params (name and color). This header means "what type of data my client sends to server".

    RestTemplate restTemplate = new RestTemplate();
    
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
    headers.add("PRIVATE-TOKEN", "xyz");
    
    MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
    map.add("name","feature");
    map.add("color","#5843AD");
    
    HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<>(map, headers);
    
    ResponseEntity<LabelCreationResponse> response =
        restTemplate.exchange("https://foo/api/v3/projects/1/labels",
                              HttpMethod.POST,
                              entity,
                              LabelCreationResponse.class);
    
    0 讨论(0)
  • 2021-02-18 22:53

    You need to set the Content-Type to application/json. Content-Type has to be set in the request. Below is the modified code to set the Content-Type

    final String uri = "https://someserver.com/api/v3/projects/1/labels";
    String input = "US";
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
    headers.add("PRIVATE-TOKEN", "xyz");
    HttpEntity<String> request = new HttpEntity<String>(input, headers);
    ResponseEntity<LabelCreationResponse> response = restTemplate.postForObject(uri, request,  LabelCreationResponse.class);
    

    Here, HttpEntity is constructed with your input i.e "US" and with headers. Let me know if this works, if not then please share the exception. Cheers!

    0 讨论(0)
提交回复
热议问题