0

When i am executing this via curl its working :

 curl -u -X DELETE -H 'accept:application/json' http://localhost:13000/test/test_userid"

I made a common function which accept methodtype ( GET, POST, DELETE etc) and content type( JASON, TEXT ) for the httpbuilder.

 def public httpRequest(String url, String content, Method requestType, ContentType contentType)
 {
        try{
            def myClient = new HTTPBuilder(url)
            myClient.request(requestType,contentType) { req ->         
            headers.'Content-Type' = 'application/json'
            body=content
            response.success = { resp, data ->           
                def reponse=[resp:resp,data:data]
                return reponse
            }

            response.failure = { resp ->
                println 'Response Code '+resp.statusLine
            }
            // called only for a 404 (not found) status code:
            response.'404' = { resp ->
                println 'Not found'
             }
            }
        }
      catch(Exception e)
       {
           println "error"+e.getProperties()
       }
}

Now if i make a POST request , its working. However if i make a GET or DELETE request using

 def response = httpRequest(url,"",DELETE,JSON)

or

 def response = httpRequest(url,"",GET,TEXT) 

its shows the following error :-

 error[message:Cannot set a request body for a DELETE/GET method, class:class java.lang.IllegalArgumentException

Do i need to make a separate function for GET/DELETE? because

 myClient.request(requestType) { req ->         
            headers.'Content-Type' = 'application/json'
            body=content
            response.success = { resp, data ->           
                def reponse=[resp:resp,data:data]
                return reponse
            }

            response.failure = { resp ->
                println 'Response Code '+resp.statusLine
            }
            // called only for a 404 (not found) status code:
            response.'404' = { resp ->
                println 'Not found'
             }
            }
        }

WORKS

1 Answers1

0

Delete and Get wont accept Body , hence the solution is to make a check and execute accordingly

 if(requestType.equals(DELETE)||requestType.equals(GET))
       {
           try{
               def myClient = new HTTPBuilder(url)
               myClient.request(requestType) { req ->          
               headers.'Content-Type' = 'application/json'
               headers.Accept = 'application/json'
               response.success = { resp, data ->          
                    def reponse=[resp:resp,data:data]
                    return reponse
               }

               response.failure = { resp ->
                   println 'Response Code '+resp.statusLine
               }
               // called only for a 404 (not found) status code:
               response.'404' = { resp ->
                   println 'Not found'
                }
               }
           }
           catch(Exception e)
           {
              println "error"+e.getProperties()
           }
       }
    else
     <---post request --> 
user2332505
  • 477
  • 1
  • 6
  • 16