cancel
Showing results for 
Search instead for 
Did you mean: 

How to get UTF-16 output in a RAW web service?

Former Member
2,682

SQL Anywhere 16.0.0.1915

I was so sure this was such a simple thing to do... LOL

I have a database created with no encoding/collation options, so it has the default 1252LATIN1 code page.

I have a simple web service of TYPE RAW. (I am STRINGing data in a JSON format to produce the readable style the consumers of the data would like to receive).

I need to produce output of type UTF-16 or UTF-8.

When I call the web service from a browser, the encoding comes out as Windows-1252.

I have tried RETURNS LONG NVARCHAR and RETURN CSCONVERT(ls_payload, 'UTF-8') but the browser still says I have Windows-1252 encoding.

I have tried CALL sa_set_http_option( 'CharsetConversion', 'On'); to make sure and CALL sa_set_http_option( 'AcceptCharset', 'UTF-8') but the browser still says I have Windows-1252 encoding.

So I am feeling like I must be missing something basic...

Thanks.

Accepted Solutions (1)

Accepted Solutions (1)

MarkCulp
Participant

There are several ways to do what you want to do...

The first thing that you are missing is that you are not telling the browser that you are sending it UTF-8 character data. To do this you need to set the Content-Type header. Example:

call sa_set_http_header( 'Content-Type', 'application/json; charset=UTF-8' );

Next you need to make sure that you actually send UTF-8 character data. The easiest (and most easily understood) way is to convert the character data explicitly. Example:

create procedure my_web_proc()
result ( rawdoc long varchar )
begin
    declare @result long varchar;
    ... -- construct your json result
    call sa_set_http_header( 'Content-Type', 'application/json; charset=UTF-8' );
    call sa_set_http_option( 'CharsetConversion', 'OFF' );  -- tell the server to NOT convert
    select csconvert( @result, 'UTF-8' );
end;

The alternative (to setting CharsetConversion to OFF) would be to describe your result set as returning binary - charset conversion is never implicitly done on binary data. Example:

create procedure my_web_proc()
result ( rawdoc long binary )
begin
    declare @result long varchar;
    ... -- construct your json result
    call sa_set_http_header( 'Content-Type', 'application/json; charset=UTF-8' );
    select csconvert( @result, 'UTF-8' );
end;

HTH

Former Member
0 Kudos

I changed my FUNCTION to a PROC and added the set_header, and now it appears to work fine. (Confirming my lack of understanding of any http functionality).

Thanks Mark!

Answers (0)