Bhargav
Bhargav

Reputation: 713

how to do post request with raw data via spring rest template

Can some one tell me how to send a POST request with raw data parameters as in the picture below enter image description here

i have tried the following code but its not working

HttpHeaders headers = new HttpHeaders();
                headers.setContentType(MediaType.APPLICATION_JSON);
            JsonObject properties = new JsonObject();
            MultiValueMap<String, String> params = new LinkedMultiValueMap<>();         
            try {

                properties.addProperty("app_id", appId);
                properties.addProperty("identity","TestAPI");
                properties.addProperty("event", "TestCompleted");
                properties.addProperty("testType", t.getTestType());
                properties.addProperty("testName",t.getTestName());
                properties.addProperty("chapter","");
                properties.addProperty("module","");
                properties.addProperty("pattern",t.getTestPattern());
                HttpEntity<String> request = new HttpEntity<>(
                        properties.toString(), headers);
               // params.add("properties", properties.toString());
                 restTemplate.postForObject(url, request, String.class);

can someone help?

Upvotes: 4

Views: 10627

Answers (4)

Bashir Zamani
Bashir Zamani

Reputation: 150

try this:

URI uri = new URI("something");
Map<String, Object> params = new HashMap<>();
        params.put("app_id", "something");
        params.put("identity", something);

HttpEntity<Map<String, String>> request = new HttpEntity(params , headers);
   
ResponseEntity<String> response = restTemplate.postForEntity(uri, request, String.class);

Upvotes: 0

mcacorner
mcacorner

Reputation: 1274

Try this :

@RestController
public class SampleController { 
    @RequestMapping("/req")
    public String performReqest(){
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);        
        JsonObject properties = new JsonObject();
        properties.addProperty("no", "123");
        properties.addProperty("name", "stackoverflow");
        HttpEntity<String> request = new HttpEntity<>(properties.toString(), headers);
        RestTemplate restTemplate = new RestTemplate();
        String response = restTemplate.postForObject("http://localhost:4040/student", request, String.class);
        return "Response from Server is : "+response;       
    }

    @RequestMapping("/student")
    public String consumeStudent(@RequestBody Student student){
        System.out.println(student);
        return "Hello.."+student.name;
    }   
}

class Student{
    public int no;
    public String name; 
    public Map<String,String> properties;   
}

Don't forgot to move Student class and change all field to private with require getters and setters. Above code is only for demo purpose.

Upvotes: 3

Mayur
Mayur

Reputation: 11

Did u tried using postmaster and checked the output first. If its working then you can go for post or exchange method. exchange returns and post dont.

Upvotes: 0

piy26
piy26

Reputation: 1592

Please try with this:

ResponseEntity<String> result = restTemplate.exchange(url, HttpMethod.POST, request, String.class);

Upvotes: 0

Related Questions