modules/tests/test-integration/src/test/java/org/ballerinalang/test/util/HttpClientRequest.java
/**
     * Send a Http POST request to a service.
     *
     * @param endpoint - service endpoint
     * @param postBody - message payload
     * @param headers http request headers map
     * @return - HttpResponse from end point
     * @throws IOException If an error occurs while sending the GET request
     */
Send a Http POST request to a service.
Send a Http POST request to a service .
public static HttpResponse doPost(String endpoint, String postBody, Map<String, String> headers)
            throws IOException {
        HttpURLConnection urlConnection = null;
        HttpResponse httpResponse;
        try {
            urlConnection = getURLConnection(endpoint);
            for (Map.Entry<String, String> e : headers.entrySet()) {
                urlConnection.setRequestProperty(e.getKey(), e.getValue());
            }
            urlConnection.setRequestMethod("POST");
            OutputStream out = urlConnection.getOutputStream();
            try {
                Writer writer = new OutputStreamWriter(out, "UTF-8");
                writer.write(postBody);
                writer.close();
            } finally {
                if (out != null) {
                    out.close();
                }
            }
            StringBuilder sb = new StringBuilder();
            BufferedReader rd = null;
            try {
                rd = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()
                        , Charset.defaultCharset()));
                String line;
                while ((line = rd.readLine()) != null) {
                    sb.append(line);
                }
            } catch (IOException e) {
                rd = new BufferedReader(new InputStreamReader(urlConnection.getErrorStream()
                        , Charset.defaultCharset()));
                String line;
                while ((line = rd.readLine()) != null) {
                    sb.append(line);
                }
            } finally {
                if (rd != null) {
                    rd.close();
                }
            }
            Map<String, String> responseHeaders = readHeaders(urlConnection);
            httpResponse = new HttpResponse(sb.toString(), urlConnection.getResponseCode(), responseHeaders);
            httpResponse.setResponseMessage(urlConnection.getResponseMessage());
            return httpResponse;
        } finally {
            if (urlConnection != null) {
                urlConnection.disconnect();
            }
        }
    }
public static HttpResponse doPost ( String endpoint , String postBody , Map < String , String > headers ) throws IOException { HttpURLConnection urlConnection = null ; HttpResponse httpResponse ; try { urlConnection = getURLConnection ( endpoint ) ; for ( Map . Entry < String , String > e : headers . entrySet ( ) ) { urlConnection . setRequestProperty ( e . getKey ( ) , e . getValue ( ) ) ; } urlConnection . setRequestMethod ( "POST" ) ; OutputStream out = urlConnection . getOutputStream ( ) ; try { Writer writer = new OutputStreamWriter ( out , "UTF-8" ) ; writer . write ( postBody ) ; writer . close ( ) ; } finally { if ( out != null ) { out . close ( ) ; } } StringBuilder sb = new StringBuilder ( ) ; BufferedReader rd = null ; try { rd = new BufferedReader ( new InputStreamReader ( urlConnection . getInputStream ( ) , Charset . defaultCharset ( ) ) ) ; String line ; while ( ( line = rd . readLine ( ) ) != null ) { sb . append ( line ) ; } } catch ( IOException e ) { rd = new BufferedReader ( new InputStreamReader ( urlConnection . getErrorStream ( ) , Charset . defaultCharset ( ) ) ) ; String line ; while ( ( line = rd . readLine ( ) ) != null ) { sb . append ( line ) ; } } finally { if ( rd != null ) { rd . close ( ) ; } } Map < String , String > responseHeaders = readHeaders ( urlConnection ) ; httpResponse = new HttpResponse ( sb . toString ( ) , urlConnection . getResponseCode ( ) , responseHeaders ) ; httpResponse . setResponseMessage ( urlConnection . getResponseMessage ( ) ) ; return httpResponse ; } finally { if ( urlConnection != null ) { urlConnection . disconnect ( ) ; } } }
Send a Http POST request to a service.
Send a Http POST request to a service .
//setting request headers // Get the response