4

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?

Bhargav
  • 697
  • 3
  • 11
  • 29
  • you can use @request body if it's structure is static – pooja patil May 04 '18 at 09:31
  • @poojapatil how? can you tell me an example? – Bhargav May 04 '18 at 09:32
  • create an Dto where you will have the above fields in it, to post use webclient , such as (client.target(end url) .request(MediaType.APPLICATION_JSON).header(if any header) .post(Entity.entity(scheme, MediaType.APPLICATION_JSON));) – pooja patil May 04 '18 at 09:39

4 Answers4

3

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.

mcacorner
  • 1,304
  • 3
  • 22
  • 45
0

Please try with this:

ResponseEntity<String> result = restTemplate.exchange(url, HttpMethod.POST, request, String.class);
piy26
  • 1,574
  • 11
  • 21
0

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.

Mayur
  • 11
  • 6
0

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);
Bashir Zamani
  • 100
  • 1
  • 2
  • 6