0

Given for example this Post request:

    Using port: 8080
    Using Webroot: /tmp
    POST /registrazione HTTP/1.1
    Content-Type: application/json
    User-Agent: PostmanRuntime/7.26.8
    Accept: */*
    Postman-Token: a628e69a-9ab1-4ef1-8b1d-7634e4dbbeab
    Host: 127.0.0.1:8080
    Accept-Encoding: gzip, deflate, br
    Connection: keep-alive
    Content-Length: 150

    {"id":0,"nome":"Rocco","cognome":"I nandu","username":"roccuzzu","email":"rock@gmail.com","password":"test123","foto":"myphoto.jpg","partecipanti":[]}

I want to take just the body of this request which in this case is a json. I'm my java code I can read all the request in this way:

    BufferedReader in = null;
    String call = null;

    try {
        inputStream = socket.getInputStream();
        outputStream = socket.getOutputStream();
        in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

        int bytes;
        while((bytes = in.read()) >=0){
            System.out.print((char) bytes);
        }

But i'm not able to take just the body. Can someone help me to do this?

Frid Ric
  • 69
  • 7
  • You should use a HTTP client library, for example from Apache. The HTT client will do the splitting for you. Your primitive code will not work with many of the common HTTP protocol options (e.g. the gzip compression which you requested (!), persistent connections, chunking, ...). – Stefan Jan 11 '21 at 16:20
  • The fact is that I can't use this kind of libraries. I need to do it with sockets – Frid Ric Jan 11 '21 at 16:35

2 Answers2

0

The headers are terminated by an empty line. You can consider only lines after the first empty one. This is valid only if the answer doesn't implement chuncked, gzip etc. as Stefan commented, but this can be ensured by the correct headers in http request. The use of a Client library is a valid alternative. HttpClient is part of Java since version 11.

Andrea
  • 306
  • 2
  • 8
  • I can't use HttpClient. Anyway I thought before about your suggestion but I'm not able to do it. I tried different implementations but they doesn't work – Frid Ric Jan 11 '21 at 16:49
  • You need to show your bad code, then we can help you to fix it. – Stefan Jan 11 '21 at 16:59
  • @Stefan I answered with new code. If you can take a look below – Frid Ric Jan 12 '21 at 14:21
  • I see that you have already a solution. use that. It may be not elegant but it is surely fast. As an alternative, you may read the whole input into a String (see https://stackoverflow.com/questions/309424/how-do-i-read-convert-an-inputstream-into-a-string-in-java) and the use the split() method of the String class. – Stefan Jan 13 '21 at 16:26
0

I've found a way to do that. It's not elegant but it works:

    private String parseBody() throws IOException {
    int bytes;
    int s = 0, e = 0;
    String request;
    StringBuilder sb = new StringBuilder();
    while((bytes = in.read()) >=0){
        if(bytes == '\r'){
            bytes = in.read();
            if(bytes == '\n'){
                bytes = in.read();
                if(bytes == '\r'){
                    bytes = in.read();
                    if(bytes == '\n'){
                        while((bytes = in.read()) >= 0){
                            sb.append((char)bytes);
                            if(bytes == '{'){
                                e = e + 1;
                            }
                            if(bytes == '}'){
                                s = s + 1;
                                if(s == e){
                                    request = sb.toString();
                                    return request;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return null;
}

Of course i'm open to any improvement and suggestion.

Frid Ric
  • 69
  • 7