CHttpClient::PostJson Method  
 
INT PostJson(
  LPCTSTR lpszResource,  
  LPCTSTR lpszJsonData,  
  LPBYTE lpResult,  
  LPDWORD lpcbResult,  
  DWORD dwOptions  
);
INT PostJson(
  LPCTSTR lpszResource,  
  LPCTSTR lpszJsonData,  
  HGLOBAL* lpResult,  
  LPDWORD lpcbResult,  
  DWORD dwOptions  
);
INT PostJson(
  LPCTSTR lpszResource,  
  LPCTSTR lpszJsonData,  
  CString& strResult,  
  DWORD dwOptions  
);

The PostJson method submits JSON formatted data to a script on the server and returns the result in a buffer provided by the caller.

Parameters

lpszResource
A pointer to a string that specifies the resource that the data will be posted to on the server. Typically this is the name of an executable script.
lpszJsonData
A pointer to a string that specifies the JSON data that will be submitted to the server.
lpResult
A pointer to a byte buffer which will contain the data returned by the server, or a pointer to a global memory handle which will reference the data when the method returns. An alternate version of this method accepts a CString object which will contain the server response.
lpcbResult
A pointer to an unsigned integer which should be initialized to the maximum number of bytes that can be copied to the buffer specified by the lpvResult parameter. If the lpvResult parameter points to a global memory handle, the length value should be initialized to zero. When the method returns, this value will be updated with the actual number of bytes of data that was returned.
dwOptions
An unsigned integer that specifies one or more options. This parameter is constructed by using a bitwise operator with any of the following values:
Constant Description
HTTP_POST_DEFAULT The default post mode. The contents of the buffer will be submitted without encoding and should use the standard JSON format. The data returned by the server is copied to the result buffer exactly as it is returned from the server.
HTTP_POST_CONVERT If the data being returned from the server is textual, it is automatically converted so that the end of line character sequence is compatible with the Windows platform. Individual carriage return or linefeed characters are converted to carriage return/linefeed character sequences. Note that this option does not have any effect on the form data being submitted to the server, only on the data returned by the server.
HTTP_POST_ERRORDATA This option causes the client to accept error data from the server if the request fails. If this option is specified, an error response from the server will not cause the method to fail. Instead, the response is returned to the client and the method will succeed.

Return Value

If the method succeeds, the return value is the server result code. If the method fails, the return value is HTTP_ERROR. To get extended error information, call GetLastError.

Remarks

The PostJson method is used to submit JSON formatted data to a script that executes on the server and then copy the output from that script into a local buffer. This function automatically sets the correct content type and encoding required for submitting JSON data to a server, however it does not parse the JSON data itself to ensure that it is well-formed. Your application is responsible for ensuring that the JSON data that is being submitted to the server is formatted correctly.

The method may be used in one of two ways, depending on the needs of the application. The first method is to pre-allocate a buffer large enough to store the resulting output of the script. In this case, the lpvResult parameter will point to the buffer that was allocated by the client and the value that the lpcbResult parameter points to should be initialized to the size of that buffer.

The second method that can be used is have the lpvResult parameter point to a global memory handle which will contain the data when the method returns. In this case, the value that the lpcbResult parameter points to must be initialized to zero. It is important to note that the memory handle returned by the method must be freed by the application, otherwise a memory leak will occur. See the example code below.

If the content type for the request has not been explicitly specified, it will be automatically updated by this function to use the "application/json" content type. You can override the default content type for the request by calling the SetHeader method prior to calling this method. Most servers require you to explicitly specify what type of data is being submitted by the client and will reject requests which do not correctly identify the content type.

It is common for servers to return additional information about a failed request in their response to the client. If you need to process this information, use the HTTP_POST_ERRORDATA option which causes the error message to be returned in the lpvResult buffer provided by the caller. If this option is used, your application should call GetResultCode to obtain the HTTP status code returned by the server. This will enable you to determine if the operation was successful.

This method will cause the current thread to block until the post completes, a timeout occurs or the operation is canceled. During the transfer, the HTTP_EVENT_PROGRESS event will be periodically fired, enabling the application to update any user interface controls. Event notification must be enabled, either by calling EnableEvents, or by registering a callback function using the RegisterEvent method.

To determine the current status of a file transfer while it is in progress, use the GetTransferStatus method.

Example

HGLOBAL hgblBuffer = (HGLOBAL)NULL;
LPBYTE lpBuffer = (LPBYTE)NULL;
DWORD cbBuffer = 0;

// Store the script output into block of global memory allocated by
// the GlobalAlloc function; the handle to this memory will be
// returned in the hgblBuffer parameter. Since the output from the
// script is textual, the HTTP_POST_CONVERT option is used
nResult = lpClient->PostJson(lpszResource,
                             lpszJsonData,
                             &hgblBuffer,
                             &cbBuffer,
                             HTTP_POST_CONVERT);

if (nResult != HTTP_ERROR)
{
    // Lock the global memory handle, returning a pointer to the
    // output data from the script
    lpBuffer = (LPBYTE)GlobalLock(hgblBuffer);
    
    // After the data has been used, the handle must be unlocked
    // and freed, otherwise a memory leak will occur
    GlobalUnlock(hgblBuffer);
    GlobalFree(hgblBuffer);
}

Requirements

Minimum Desktop Platform: Windows 7 (Service Pack 1)
Minimum Server Platform: Windows Server 2008 R2 (Service Pack 1)
Header File: cstools10.h
Import Library: cshtpv10.lib
Unicode: Implemented as Unicode and ANSI versions.

See Also

EnableEvents, GetData, GetTransferStatus, PostData, PostXml, PutData, RegisterEvent