Home > Mobile >  Pass a string in payload of POST
Pass a string in payload of POST

Time:04-26

Usually we pass key-value pair in JSON payload for POST/PUT request. Is it possible to pass a sting only ex: enter image description here

If so what do we set the object for @RequestBody? Would it be String type or JSONObject?

CodePudding user response:

I did this:

@PostMapping(value = "businessdate", consumes = MediaType.TEXT_PLAIN_VALUE)
public void postBusinessDate(@RequestBody String businessDate) throws IOException, InterruptedException, SQLException {
    businessDateService.updateBusinessDate(LocalDate.parse(businessDate));
}

and passed this:

enter image description here

CodePudding user response:

enter image description here

It would be a String. Even though you choose content type as application/json

enter image description here

CodePudding user response:

What worked for me is setting

@PutMapping(value = "/test/{id}", consumes = MediaType.APPLICATION_JSON_VALUE)
String updateInfo(@RequestHeader(@PathVariable("id") String id, @RequestBody String payload){...}

and passing the string payload by escacping the double quotes

updateInfo(token, id, "\"TEST\"");
  • Related