Submitting Data to Web Services

When you want to submit data to a web service, there are several different approaches that you can take in your applications. Code which was written using older versions of SocketTools would simply use the PostData method, or HttpPostData function in the API. With the current version, you have additional options.

The PostData Method

In earlier versions of SocketTools, there was only one way to submit payload data to a web service, and that was using the PostData method or HttpPostData function. It is a general use method which can issue a POST request and provide any kind of data payload provided.

It accepts either a string or byte array as the payload and will perform the appropriate encoding based on the encoding type specified for the session. By default, most form-based payloads would require URL encoding and the content type would be specified as "application/x-www-form-urlencoded".

Here is a simple example using the SocketTools.HttpClient .NET class:

In this example, a request is being sent to our test server at test.sockettools.com with a simple text payload. Because there are no options set, it will be URL encoded and submitted using POST. The response from the server will be returned in a string, and in this case, will contain some diagnostic output, along with a hex dump of the payload which was submitted:

What if the service does not expect the payload to be URL encoded? This is usually the case today where the request payload consists of XML or JSON formatted data. You would do this by setting the Encoding property prior to submitting the request (or in the case of the library API, calling the HttpSetEncodingType function).

Now the payload does not contain any encoding when it's sent to the server for processing:

There is also a special XML encoding type which would use URL encoding, except spaces would not be encoded. However, most services today which expect an XML payload will not use this (and in fact, will not recognize URL encoding at all).

PostData and Unicode Strings

The PostData method can accept both byte arrays and string buffers as the payload which will be submitted to the server. When a byte array is provided, the data is sent as-is without any modification. When a Unicode string contains the payload, it will be automatically encoded as UTF-8 text prior to being submitted.

This is an important consideration when sending text which contains non-ASCII characters because what is sent may not be identical to what is received by the server. Any character outside of the standard ASCII range will be encoded as a multi-byte character sequence which can represent the Unicode characters in the string.

Most web services expect text to use UTF-8 encoding and this does not present a problem in typical use cases. Legacy web services which do not support UTF-8 may expect text to use a specific ANSI character set, such as ISO-Latin 1 and in that case, your application will need to convert your Unicode string into a byte array using that specific character set, and then provide the byte array as the payload argument.

In .NET, refer to the System.Text.Encoding related classes to help perform that conversion. With the ActiveX controls, you can use the StrConv function in Visual Basic to convert a string to a byte array. With C/C++ you can use the WideCharToMultiByte function to perform the conversion.

In summary, if you are working with string data and want to prevent it from being sent as UTF-8 encoded text, convert it to a byte array first and that ensures it will be submitted "as is" to the server.

PostJson and PostXml

To make things a bit simpler when submitting JSON and XML formatted payloads, the current version of SocketTools includes two methods named, as expected, PostJson and PostXml. The API includes equivalent functions named HttpPostJson and HttpPostXml.

These work similarly to PostData and HttpPostData, however they do a few important things which eliminates some extra coding which would otherwise be required:

The JSON and XML payloads are automatically encoded as UTF-8 text, making it safe to submit data which contains Unicode characters.

The Content-Type request header is automatically set for the correct content type. For JSON payloads it will be set to "application/json" and for XML it will be set to "text/xml". You can override those values if you wish using the SetHeader method or HttpSetRequestHeader function, but it is not recommended.

The default encoding type is set to "none" so the data will not be submitted using the same URL encoding method common to form-related payloads. This is expected by virtually every RESTful web service today.

Here is an example of a simple JSON request using the .NET class:

As you can see, this involves a bit less code and makes it clear exactly what your application is doing (in this case, submitting a JSON request to the server).

To help you test your code, we have the test.sockettools.com server which can be used to test and help debug different types of requests, including the most common GET, POST and PUT operations.

See Also

Using the SocketTools Test Server
Automatic URL Redirection
Unicode Support in SocketTools