Skip to content

Class: cJsonHttpTransfer

Properties | Events | Methods | Index of Classes

Provides an easy mechanism for sending and receiving JSON documents via HTTP

Hierarchy

cObject > InetTransfer > cBaseHTTPTransfer > cHttpTransfer > cJsonHttpTransfer

Show full hierarchy and direct subclasses

Library: Common Class Library

Package: cJsonHttpTransfer.pkg

Description

This class provides an easy mechanism for sending and receiving JSON (JavaScript Object Notation) documents via HTTP. JSON documents are sent and retrieved as JSON objects.

This class is dependent on the cJsonObject class. It is expected that this class always uses cJsonObject object in and out parameters. JSON being sent will be created using the cJsonObject class interface and JSON being returned will be analyzed using the cJsonObject class interface.

This class can be used to transfer data in a RESTful manner. Interfaces exist allowing you to use all of the common HTTP verbs - HttpDeleteJson, HttpGetJson, HttpPatchJson, HttpPostJson and HttpPutJson. An additional method HttpVerbJson can be used where you can pass any verb of your choosing.

Sample

Here is an example of transferring data as JSON objects using HttpPostJson:

// returns the response JSON data. If 0, no data but legal,
// if -1, bad transfer
Function TransferMyData Handle hoReqJson Returns Handle
    Handle hoHttp hoRcvdJson
    Boolean bOk

    // Create JSON HTTP transfer object.
    Get Create (RefClass(cJsonHttpTransfer)) to hoHttp
    // post JSON document and receive returned JSON document
    Get HttpPostJson of hoHttp "dataaccess.com" ;
        "MyApplication/JsonService.asp" hoReqJson (&bOk) to hoRcvdJson
    // transfer object no longer needed
    Send Destroy of hoHttp
    // it's actually possible for a valid transfer to not return an object 
    // if a transfer error, we return -1 (remember this is an example)
    If not (bOk) Begin
        Move -1 to hoRcvdJson       
    End
    Function_Return hoRcvdJson // if >0, this is newly created, and must be destroyed
End_Function

The JSON data objects are passed and returned as UTF-8 data over HTTP. This is what would be expected for JSON transfers over HTTP.

Transfer errors can be further analyzed by using the peJsonTransferStatus property.

This class allows you to transfer and receive data that is larger than the string size limitations imposed by DataFlex. It does this by augmenting OnDataReceived and uses UChar arrays to manipulate the data.

You may wish to study the class source code to see how this is done, so you can create other cHttpTransfer subclasses that allow you to transfer large amounts of data. Also note that the event, OnDataReceived, is not a public event in this class - you should not change it.

A similar class, cXmlHttpTransfer exists for transferring XML documents using XML.