0

I'm trying to send a JSON that contains a map in a POST request using postman. I've already tried numerous combinations, unfortunately to no avail.

I set Content-Type to application/json. I've seen this question: Sending map in postman post request, but the advice provided there doesn't help.

Here's what I've tried so far: Key is payload, then Value is:

  1. {message: message1, country:germany},
  2. {"message": message1, "country":germany},
  3. {"message": "message1", "country":"germany"},
  4. Some other options with numerous escaping attempts - none of them works.

Here are some java classes that are supposed to handle this:

    @RequestMapping(method = RequestMethod.POST, value = "/send/singleUserReq")
    @ResponseBody
    public String sendNotificationToSingleUser(@RequestBody SingleUserRequest singleUserRequest) {
        //...
    }
public abstract class Request {

    private String deviceType;
    private Map<String, String> payload;
    private String appId;
}
public class SingleUserRequest extends Request {

    private String deviceId;
}

EDIT:

When I send the following request with postman:

http://localhost:8080/api/send/singleUserReq?deviceType=ios&payload={"message": "message1", "country":"germany"}&appId=appName1&deviceId=12345

I get this error on the java side: Invalid character found in the request target [/api/send/singleUserReq?deviceType=ios&payload={%22message%22:%20%22message1%22,%20%22country%22:%22germany%22}&appId=appName1&deviceId=12345]. The valid characters are defined in RFC 7230 and RFC 3986

SkogensKonung
  • 601
  • 1
  • 9
  • 22
  • A JSON like `"payload" : {"aKey" : "aValue", "anotherKey" : "anotherValue"}` is quite good for a variable `Map payload`. So what problem do you actually have? – Seelenvirtuose Jul 27 '21 at 18:04
  • When I send such a request, I get `HTTP Status 400 – Bad Request`. – SkogensKonung Jul 27 '21 at 19:20
  • Can you show the complete api request JSON that you're passing in the postman request? – Gaurav kumar Singh Jul 27 '21 at 20:12
  • I edited my post so that it contains the complete request msg and the corresponding java error. – SkogensKonung Jul 27 '21 at 20:23
  • 1
    your request is incorrect. you're sending requestparam but the api is expecting requestbody. https://stackoverflow.com/questions/28039709/what-is-difference-between-requestbody-and-requestparam#:~:text=%40RequestParam%20makes%20Spring%20to%20map,request%20to%20your%20method%20argument.&text=%40RequestBody%20makes%20Spring%20to%20map,its%20getter%20and%20setter%20methods. – Gaurav kumar Singh Jul 27 '21 at 20:51

1 Answers1

0

Looks like you're are not sending the correct JSON in the request. As per your rest endpoint, the JSON should be of type SingleUserRequest.

{
    "deviceId" : "<your-device-id>",
    "deviceType" : "<your-device-type>",
    "payload" : {"message": "message1", "country":"germany"},
    "appId" : "<your-app-id>"
}