Class PostRequest


  • public class PostRequest
    extends HttpRequest

    Provides a POST Request class for performing simple HTTP request operations. This class provides an easier and safer interface for the Apache HTTP Components library.

    The default connection timeout is 10 seconds and socket read timeout is 60 seconds. Socket connections will also apply any JVM proxy settings automatically.

    By default the maximum response read size is 16 MB. If the response is larger than this an IOException will be thrown. To increase the maximum read limit use the HttpRequest.setReadLimit(int) method.

    In RESTful services the POST method is used to create resources (however its often used to perform updates as well).

    Examples

    The example below performs POST request and converts the response into a JSON document.

     import com.avoka.tm.http.*
     import com.avoka.tm.util.*
    
     String message = '''{
         "name":"John Doe",
         "email":"[email protected]",
         "age":42
     }'''
    
     String username = svcDef.paramsMap.username
     String password = svcDef.paramsMap.password
    
     // execute POST request and return a HttpResponse object
     HttpResponse response = new PostRequest('https://service.mycorp.com/secure/rest/accounts/')
           .setMessage(message)
           .setBasicAuth(username, password)
           .execute()
    
     // check HttpResponse status code
     if (!response.isStatusOK()) {
         throw new RuntimeException(response.statusLine)
     }
    
     // get JSON text from the response
     String json = response.getTextContent()
     ... 
    Since:
    5.0.0
    See Also:
    DeleteRequest, GetRequest, PatchRequest, PutRequest
    • Constructor Detail

      • PostRequest

        public PostRequest​(java.lang.String url)
        Create a POST HTTP request object with the given URL.
        Parameters:
        url - the request URL (required)