CHttpClient::SetEncodingType Method  
 
INT SetEncodingType(
  INT nEncodingType  
);

The SetEncodingType method specifies the type of encoding to be applied to the content of a HTTP request.

Parameters

nEncodingType
Specifies the content encoding type for the request. The following values are defined:
Value Description
HTTP_ENCODING_NONE No encoding will be applied to the content of a request and no default content type will be specified.
HTTP_ENCODING_URL Non-printable and extended ASCII characters will be encoded so they can be safely used with URLs and form data. Encoded characters will be represented by a percent symbol prefix, followed by a two digit hexadecimal value which represents the ASCII character code. This encoding is typically used with web services which process HTML form data. If the content type has not been explicitly specified, it will default to application/x-www-form-urlencoded.
HTTP_ENCODING_XML This encoding specifies the content being submitted to the server contains XML structured data. If the content type has not been defined, it will default to application/xml, which is the official MIME media type for XML text. Legacy services may expect text/xml as the content type.
HTTP_ENCODING_JSON This encoding specifies the content being submitted to the server contains JSON structured data. If the content type has not been defined, it will default to application/json, which is the official MIME media type for JSON text. In rare cases, legacy services may expect text/x-json as the content type.
HTTP_ENCODING_UTF8 This encoding specifies the content is unstructured text which has been UTF-8 encoded. If the content type has not been defined, it will default to text/plain; charset=utf-8 which is appropriate for any kind of general text payloads. If you use the Unicode version of the methods in this library, all text is normally submitted as UTF-8 encoded text.
HTTP_ENCODING_ANSI This encoding specifies the content is unstructured text and may not be UTF-8 encoded if the ANSI version of the methods in this library are used. This encoding is not recommended unless there is a specific need to send text using a specific character set. If the content type has not been defined, it will default to plain/text.
HTTP_ENCODING_XMLURL This encoding is identical to URL encoding, except spaces are not encoded. It is used with legacy web services which expect form data in an XML format and cannot process encoded whitespace. This encoding should not be specified for services which use REST APIs. If the content type has not been specified, it will default to application/x-www-form-urlencoded.

Return Value

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

Remarks

The SetEncodingType method sets the type of encoding used when optional parameter data is submitted with a request for a resource. If you do not set the Content-Type request header prior to submitting the request, the default content type provided to the server will be determined by the encoding you choose. If you specify HTTP_ENCODING_NONE, no default content type will be provided. Because most services require a Content-Type header as part of a POST request, this can cause the server to reject the request. The SetContentType method can be used to explicitly specify the content type for the request.

If the service you are connected with expects an XML formatted request, it is recommended you use the PostXml method. If the service expects JSON formatted text, use the PostJson method. These methods ensure the correct encoding and content type are always used. If the service expects some other kind of text payload, you can set the appropriate content type using the SetContentType method and then use the PostText method to submit the request.

If you are using the PostData method to submit form data to the server, in most cases you should use HTTP_ENCODING_URL. If the form data contains XML text, you may need to use the HTTP_ENCODING_XMLURL encoding type. However, in most cases, this is not the appropriate encoding type for XML and should never be used when submitting XML to a REST API endpoint.

If you are submitting a payload which contains binary data, you would typically use HTTP_ENCODING_NONE and explicitly set the appropriate content type using the SetContentType method. For example, if you're submitting a JPEG image to a REST endpoint, you should not use any encoding and set the Content-Type header to image/jpeg. If your application needs to set the content type to a non-standard value for a proprietary API, use the SetHeader method to set the Content-Type header value. That method will not perform any validation on the header value.

Requirements

Minimum Desktop Platform: Windows 7 Service Pack 1
Minimum Server Platform: Windows Server 2008 R2 Service Pack 1
Header File: cstools12.h
Import Library: cshtpv12.lib

See Also