Receiving HTTP POST request in Groovy

1

I have created a simple server which accepts connection request forms the client and reads the request posted by client. Depends upon some values in the request body I would like to respond with some string. and if the server does not receive any requests from the client it just sent some integer values to the client. The following is the code.

def server = new ServerSocket(2000)
println("Waiting for connection")

String receivedRecord ;
while (true) 

{
  println " Input data "
server.accept() { socket ->
          socket.withStreams { input, output ->
                def w = new BufferedWriter(new OutputStreamWriter(output))

        String message = "Connection was successful"
        println message
 def r = new BufferedReader(new InputStreamReader(input))

 println ("Stream ")

String requestMessage = ""
 while (true)
  { 
        String tempMsg = r.readLine() 
        requestMessage = requestMessage  +   tempMsg
        println (requestMessage)
   }       

      int  i = 0;

while (i <20)
{
    //String requestMessage = r.readLine()

    //println(" Request Received ")
    //println(requestMessage)

    String echo = "EchoRequestMsg"
    String vehicleSync = "EenheidSynchronisatieRequestMsg"

    if(requestMessage != null )
    {

        if(requestMessage.toLowerCase().contains(echo.toLowerCase()))
        {
            println (i)
            println ("Echo Request True ")
            sendMessage("Echo", w)
            println(" Echo Message Sent ")

        }
        else if(requestMessage.toLowerCase().contains(vehicleSync.toLowerCase()))
        {     
            println (i)
            println (" Vehicle Sync True ")       
            sendMessage("VehicleSync", w)
            println(" Vehicle Sync Message Sent ")
        }
        else
        {
            println (i)
            println (" push message ")
            i++;
            sendMessage(i, w)
        }

       }   
      }
     }
    }
   }
def sendMessage(msg, writer) {
    println("Sending: >" + msg + "<")
    writer.writeLine(msg.toString())
    writer.flush();
}

It only receive http Header not the body. I have posted the request thru cURL.

Traced Data from cURL:

== Info: Rebuilt URL to: http://localhost:2000/
== Info:   Trying ::1...
== Info: Connected to localhost (::1) port 2000 (#0)
=> Send header, 0000000122 bytes (0x0000007a)
0000: 50 4f 53 54 20 2f 20 48 54 54 50 2f 31 2e 31 0d POST / HTTP/1.1.
0010: 0a 48 6f 73 74 3a 20 6c 6f 63 61 6c 68 6f 73 74 .Host: localhost
0020: 3a 32 30 30 30 0d 0a 41 63 63 65 70 74 3a 20 2a :2000..Accept: *
0030: 2f 2a 0d 0a 63 6f 6e 74 65 6e 74 2d 74 79 70 65 /*..content-type
0040: 3a 74 65 78 74 2f 78 6d 6c 0d 0a 53 4f 41 50 41 :text/xml..SOAPA
0050: 63 74 69 6f 6e 3a 75 72 6e 3a 67 65 74 42 61 6e ction:urn:getBan
0060: 6b 0d 0a 43 6f 6e 74 65 6e 74 2d 4c 65 6e 67 74 k..Content-Lengt
0070: 68 3a 20 33 31 31 0d 0a 0d 0a                   h: 311....
=> Send data, 0000000311 bytes (0x00000137)
0000: 3c 73 6f 61 70 65 6e 76 3a 45 6e 76 65 6c 6f 70 <soapenv:Envelop
0010: 65 20 78 6d 6c 6e 73 3a 73 6f 61 70 65 6e 76 3d e xmlns:soapenv=
0020: 22 68 74 74 70 3a 2f 2f 73 63 68 65 6d 61 73 2e “xxxxxxxxxxxxxx
0030: 78 6d 6c 73 6f 61 70 2e 6f 72 67 2f 73 6f 61 70 xmlsoap.org/soap
0040: 2f 65 6e 76 65 6c 6f 70 65 2f 22 20 78 6d 6c 6e /envelope/" xmln
0050: 73 3a 76 31 3d 22 68 74 74 70 3a 2f 2f 67 6d 73 s:v1="http://gms
0060: 2e 70 6f 6c 69 74 69 65 2e 6e 6c 2f 62 72 6f 6b .xxxxxxxxx/brok
0070: 65 72 2f 77 65 62 73 65 72 76 69 63 65 2f 6d 6f er/webservice/mo
0080: 64 65 6c 2f 76 31 22 3e 3c 73 6f 61 70 65 6e 76 del/v1"><soapenv
0090: 3a 48 65 61 64 65 72 2f 3e 20 3c 73 6f 61 70 65 :Header/> <soape
00a0: 6e 76 3a 42 6f 64 79 3e 20 3c 76 31 3a 45 63 68 nv:Body> <v1:Ech
00b0: 6f 52 65 71 75 65 73 74 4d 73 67 3e 20 3c 41 66 oRequestMsg> <Af
00c0: 7a 65 6e 64 65 72 3e 20 3c 43 6f 64 65 3e 20 58 zender> <Code> X
00d0: 58 58 58 58 20 3c 2f 43 6f 64 65 3e 20 3c 54 79 XXXX </Code> <Ty
00e0: 70 65 3e 20 58 58 58 58 58 20 3c 2f 54 79 70 65 pe> XXXXX </Type
00f0: 3e 20 3c 2f 41 66 7a 65 6e 64 65 72 3e 20 3c 2f > </Afzender> </
0100: 76 31 3a 45 63 68 6f 52 65 71 75 65 73 74 4d 73 v1:EchoRequestMs
0110: 67 3e 20 20 3c 2f 73 6f 61 70 65 6e 76 3a 42 6f g>  </soapenv:Bo
0120: 64 79 3e 20 3c 2f 73 6f 61 70 65 6e 76 3a 45 6e dy> </soapenv:En
0130: 76 65 6c 6f 70 65 3e                            velope>
== Info: upload completely sent off: 311 out of 311 bytes

Input Received by Groovy script:

    groovy groovyServer.groovy 
Waiting for connection
 Input data 
 Input data 
Connection was successful
Stream 
POST / HTTP/1.1
Host: localhost:2000
Accept: */*
content-type:text/xml
SOAPAction:urn:getBank
Content-Length: 311

Any help would be appreciated. Thanks.

curl
groovy
http-post
inputstream
asked on Stack Overflow Jan 4, 2017 by Kid • edited Jan 4, 2017 by shellter

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0