Skip to content

Class: cHttpClient

Include: Use System\Http.pkg

HTTP client for sending requests, managing headers/options/certificates, and reading responses.

Hierarchy

Package: System\Http.pkg

Description

cHttpClient sends HTTP requests, manages endpoint/proxy/headers/options/client certificates, exposes response status/headers/body helpers, can stream responses to files, and can map JSON responses to DataFlex data types.

Request lifecycle

  • Set psEndpoint or pass an absolute URL path.
  • Set headers, options, and any request body.
  • Call an HTTP method that returns HttpStatus.
  • Inspect Err for transport or request setup errors.
  • Inspect the returned status for the HTTP response status.
  • Read response helpers such as HttpResponseBody, HttpResponseHeaders, or HttpResponseToDataType.

Examples

Basic GET

Procedure BasicGet
    Handle hoObj
    HttpStatus eStatus
    String sBody sContentType
    UChar[] ucBody
    tHttpHeader[] aResponseHeaders

    Get Create (RefClass(cHttpClient)) to hoObj
    Set psEndpoint of hoObj to C_HTTP_ENDPOINT
    Set HttpHeader of hoObj to "Demo" "Value"

    Get HttpGet of hoObj "/get" to eStatus

    Get HttpResponseBody of hoObj to sBody
    Get HttpResponseBodyUC of hoObj to ucBody
    Get HttpResponseHeaders of hoObj to aResponseHeaders
    Get HttpResponseContentType of hoObj to sContentType

    Send Destroy of hoObj
End_Procedure

POST a DataFlex struct and read a JSON response

Struct HttpDT1
    { Name="username" }
    String sUsername
    { Name="attributes" }
    String[] asAttributes
End_Struct

Struct HttpBinResultDT1
    { Name="origin" }
    String sOrigin
    { Name="url" }
    String sUrl
    { Name="data" }
    String sData
    { Name="json" }
    HttpDT1 oJson
End_Struct

Procedure PostDataType
    Handle hoObj
    HttpStatus eStatus
    HttpDT1 oUsr oRetUsr
    HttpBinResultDT1 oRet
    String sUserName

    Get Create (RefClass(cHttpClient)) to hoObj
    Set psEndpoint of hoObj to C_HTTP_ENDPOINT

    Move "demo" to oUsr.sUsername
    Move "group1" to oUsr.asAttributes[-1]
    Move "group2" to oUsr.asAttributes[-1]
    Move "group3" to oUsr.asAttributes[-1]

    Get HttpPost of hoObj "/post" oUsr to eStatus
    Get HttpResponseToDataType of hoObj to oRet
    Get HttpResponseToDataType of hoObj False "json" to oRetUsr
    Get HttpResponseToDataType of hoObj False "json.username" to sUserName

    Send Destroy of hoObj
End_Procedure

Request and response behavior

Status and errors

HTTP response status is returned as HttpStatus. Transport or request setup errors are reported through the standard DataFlex error mechanism.

Automatic request body serialization

String bodies use text/plain; UChar[] bodies use application/octet-stream; structs, arrays, and other Variant values are serialized as JSON and use application/json. Explicit sContentType overrides the automatic content type. An existing Content-Type request header is respected when no explicit sContentType is passed.

JSON response deserialization

HttpResponseToDataType is the matching feature for JSON response bodies. It reads the current response body, infers the destination variable/struct/array type from the receiving variable, honors bOptRequireAll, and can select a nested value with dotted paths such as json.username.

File and chunked responses

*ToFile methods write response bytes directly to a file and do not populate HttpResponseBody. pbChunked=True delivers response bytes through OnChunk and does not populate HttpResponseBody.