0

I am using POSTMAN to test an api I have written in JAVA. I am able to access the api itself (I see it when I debug), but the JSON object I am sending is received as empty. At the end of execution, an empty print is happening.

In the header configurations I have the following: enter image description here

and in the body section I entered the following json: enter image description here

My Java code:

@POST
@Path("admin/save'")
public Response printInputText(@Context SecurityContext sc,InputStream incomingData) {

        String requestText = getStringFromInputStream(incomingData);
        System.out.println(requestText);
        if(requestText.equals("")){
            return Response.status(Status.INTERNAL_SERVER_ERROR).build();   
        }
        return Response.status(Status.OK).build();

}

public static String getStringFromInputStream(InputStream is) {

    BufferedReader br = null;
    StringBuilder sb = new StringBuilder();
    String line;
    try {
        br = new BufferedReader(new InputStreamReader(is));
        while ((line = br.readLine()) != null) {
            sb.append(line + System.lineSeparator());
        }

    } catch (IOException e) {
        e.getMessage();
        e.printStackTrace();
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return sb.toString();
}

curl (changed the url a bit but I am getting to the url so it is not the issue):

curl --location --request POST 'http://uri.com/ui/rest/admin/save' \

--header 'Authorization: Basic QzUzMDY3Nzc6QWxlWEZlckczMg==' \

--header 'Content-Type: application/json' \

--data-raw '{ "key": "value", "nextKey": "nextVal", "lastKey": "lastVal" }'

Uri Shapira
  • 369
  • 1
  • 2
  • 17
  • URL you are trying to hit in POSTMAN? – ProGamer Jun 18 '20 at 06:21
  • Can you share curl from postman here? – Dhiral Kaniya Jun 18 '20 at 06:30
  • I'd rather not specify the exact url, but my request is getting to it as I can see when I debug in Eclipse. I added the curl from postman. thanks! – Uri Shapira Jun 18 '20 at 06:40
  • I guess requestText is json content with String. How did you return the response to postman? – Gurkan İlleez Jun 18 '20 at 06:50
  • I'm assuming you're using Spring here because you didn't specify. Perhaps your InputStream is already being consumed by another part of the framework? https://stackoverflow.com/questions/8522568/why-is-httpservletrequest-inputstream-empty – Nicko Jun 18 '20 at 07:15
  • I am indeed using Spring, sorry for the confusion. The thing is that the method printInputText() is the api itself. The stream is received by the api and undergoes no processing in the Java code beforehand – Uri Shapira Jun 18 '20 at 07:19

0 Answers0