CHttpClient::SetContentType Method  
 
BOOL SetContentType(
  LPCTSTR lpszContentType  
);

The SetContentType method sets the content type for the next request.

Parameters

hClient
Handle to the client session.
lpszContentType
A pointer to a null terminated string which contains the MIME content type of the data being submitted to the web server. If this parameter is NULL or an empty string, the current type will be cleared and the client will use a default type based on the request and content encoding.

Return Value

If the method succeeds, it will return a non-zero value. If the method fails, it will return zero. To get extended error information, call HttpGetLastError.

Remarks

The content type string must use the standard IANA format which consists of a type and subtype separated by a forward slash. Some common content types are:

Content Type Description
text/plain The request payload is unstructured text in a human readable format. This content type serves a default for textual data which does not explicitly have another standard subtype.
text/csv The request payload is in a comma-separated value (CSV) text format.
application/octet-stream The request payload contains binary data. This content type serves a default for binary data which does not explicitly have another standard subtype.
application/json The request payload is standard JSON formatted text. This is the default content type used when submitting JSON requests, however some servers may expect the client to use the non-standard text/json type.
application/xml The request payload is standard XML formatted text. This is the default content type used when submitting XML requests, however some servers may expect the client use the deprecated text/xml type. The standard content type for XML is defined in RFC 7303, however most servers will recognize both types as valid.

If a content type is not explicitly specified with a request, an appropriate default content type will be automatically selected. For example, the PostJson method will set the content type to application/json by default. This method can be used to override that default value.

This method will validate the content type, but does not validate the subtype. Unregistered subtypes should always be prefixed with "x-" which designates it as experimental. Some commonly used types also use this designation, such as text/x-vcalendar for the vCalendar format.

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 and will include it in the request as-is.

The contents of the payload is not checked to ensure the content type is valid. It is the responsibility of the application to ensure that the correct content type is used when submitting the request.

Requirements

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

See Also

GetContentType, GetHeader, SetHeader