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.

asked 01 Nov '16, 12:34

Bill%20Aumen's gravatar image

Bill Aumen
2.1k354775
accept rate: 16%


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

permanent link

answered 01 Nov '16, 12:57

Mark%20Culp's gravatar image

Mark Culp
24.9k10139297
accept rate: 41%

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!

(01 Nov '16, 15:47) Bill Aumen
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×40
×13
×2

question asked: 01 Nov '16, 12:34

question was seen: 2,018 times

last updated: 01 Nov '16, 15:47