BBWebClient Utility Overview

Description

In BBj 18.0 and higher, the BBWebClient Utility provides an easy way to retrieve content from web servers by wrapping Apache's HttpClient class. The utility allows you to download web pages and interface with REST web services by supporting HTTP GET, POST, PUT, and DELETE methods.

Example

rem 'USE statements

use ::REST/BBWebClient.bbj::BBWebClient
use ::REST/BBWebClient.bbj::BBWebRequest
use ::REST/BBWebClient.bbj::BBWebResponse

rem 'GET JSON content from a simple HTTP request & response service
serverUrl$ = "http://httpbin.org/get"
client! = new BBWebClient()
request! = new BBWebRequest()
request!.addHeader("Accept", "application/json")
request!.setMethod("GET")
request!.setURI(serverUrl$)
response! = client!.sendRequest(request!)

rem 'Print out the status code
statusCode = response!.getStatusCode()
print "Requesting data from '" + serverUrl$ + "' resulted in a status code of", statusCode

rem 'Print out the headers
headers! = response!.getHeaders()
for i = 1 to headers!.size()-1
    header! = headers!.get(i)
    print " Header", i, ": ", header!.getName(), ": ", header!.getValue()
next

rem 'Print out the body contents
print "Content: ",  response!.getBody()

rem 'Close the response and end
response!.close()
end

Example Program Output

Requesting data from 'http://httpbin.org/get' resulted in a status code of 200
Header 1: Server: gunicorn/19.8.1
Header 2: Date: Wed, 27 Jun 2018 23:35:31 GMT
Header 3: Content-Type: application/json
Header 4: Content-Length: 239
Header 5: Access-Control-Allow-Origin: *
Header 6: Access-Control-Allow-Credentials: true
Header 7: Via: 1.1 vegur
Content: {"args":{},"headers":{"Accept":"application/json","Accept-Encoding":"gz
ip,deflate","Connection":"close","Host":"httpbin.org","User-Agent":"Apache-HttpC
lient/4.5.5 (Java/1.8.0_161)"},"origin":"73.26.138.141","url":"http://httpbin.or
g/get"}
READY
>

 

For complete BBWebClient documentation, click here.

See Also

BBJSONResolver