Sending a request through HTTPClient or RESTClient with a special character results in an error

When sending a request with the HTTPClient or RESTClient, if there is a "special" character (e.g. Napoléon) and you also set the "Content-Length" in the request header, you will get "the request doesn’t reach the server" issue. The request is successful if you remove the "special" character.

Here is the sample code:

ls_url ="http://localhost/invoice/n_webservice.asmx"
ls_soap_action= 'http://tempurl.org/of_sayhi'
lo_client = Create httpClient
lo_client.SetRequestHeader("Content-Type", "text/xml;charset=UTF-8")
lo_client.SetRequestHeader("SOAPAction", ls_soap_action)
lo_client.SetRequestHeader("Content-Length", String(len(ls_body) ))
lo_client.Timeout = 15
lo_client.sendrequest('POST',ls_url,ls_body, EncodingUTF8!)

The content of the request sent:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <of_sayhi xmlns="http://tempurl.org">
      <as_name>Napoléon Bonaparte</as_name>
    </of_sayhi>
  </soap:Body>
</soap:Envelope>

The execution of the code above for sending the request will fail. The request will be successful if "Napoléon" is removed from the content.

Environment

PowerBuilder 2017 R3 and later

Cause

The submitted Content-Length is not correct if your are using some "special" characters. Remember UTF-8 is not fixed sized per character. The content-length has to be the length in bytes (octets).

Solution

When "special" characters are included in the request content, do not set the "Content-Length" in the request header.  For example, comment the line that set the "Content-Length":

//lo_client.SetRequestHeader("Content-Length", String(len(ls_body) ))