8

I want to send an UTF8 json string via utl_http to a node.js Server via POST. Sending the string

["Sant Julià de Lòria"]

does arrive as

["Sant Juli� de L�ria"]

The PL/SQL code goes like:

FUNCTION http_post_varchar(
    p_url          VARCHAR2,
    p_request_body VARCHAR2 )
  RETURN VARCHAR2
AS
  req   UTL_HTTP.REQ;
  resp  UTL_HTTP.RESP;
  value VARCHAR2(32767);  -- URL to post to
  v_url VARCHAR2(200) := p_url;
  -- Post Parameters
  v_param VARCHAR2(32767) := p_request_body;
  v_param_length NUMBER := length(v_param);
BEGIN
  req := UTL_HTTP.BEGIN_REQUEST (url=> v_url, method => 'POST');
  UTL_HTTP.SET_HEADER (r      =>  req,
                       name   =>  'Content-Type',
                       value  =>  'application/json;charset=UTF-8');
  UTL_HTTP.SET_HEADER (r      =>   req,
                       name   =>   'Content-Length',
                       value  =>   v_param_length);
  UTL_HTTP.WRITE_TEXT (r      =>   req,
                       data   =>   v_param);

  resp := UTL_HTTP.GET_RESPONSE(req);
  LOOP
    UTL_HTTP.READ_LINE(resp, value, TRUE);    
  END LOOP;
  UTL_HTTP.END_RESPONSE(resp);
  RETURN 'OK';
EXCEPTION
  WHEN UTL_HTTP.END_OF_BODY THEN
    UTL_HTTP.END_RESPONSE(resp);
  RETURN 'OK';
END http_post_varchar;
4
  • What is your database charset? What is your session nls_lang charset? Commented Feb 18, 2014 at 11:07
  • CHARACTER SET: AL32UTF8 NLS_CHARACTERSET: GERMAN Commented Feb 18, 2014 at 11:08
  • 1
    Are you sure you're reading the response as an UTF8 string? It looks as if you tried to read an UTF8 string with a 8-bit charset (ò => �) Commented Feb 18, 2014 at 11:15
  • Is it possible to achieve this with NLS_CHARACTERSET = WE8MSWIN1252 ? Commented Jun 8, 2023 at 15:44

3 Answers 3

13

You should change your code to:

UTL_HTTP.SET_BODY_CHARSET('UTF-8');
UTL_HTTP.SET_HEADER (r      =>   req,
                     name   =>   'Content-Length',
                     value  =>    LENGTHB(v_param));

UTL_HTTP.WRITE_RAW (r    => req,
                    data => UTL_RAW.CAST_TO_RAW(v_param));

LENGTHB for byte length because of UTF-8. Otherwise the calculated length will be false and you get an error on the target side (unexpected end of input or something).

Sign up to request clarification or add additional context in comments.

2 Comments

I made it finally work by additionally adding "utl_http.set_body_charset('UTF-8');" Attention: Once executed this setting is valid for the whole session, even if you remove it for testing, it will still work.
added the line to the solution
4

Same goes for the response body e.g. when you're getting a response from a web service:

UTL_HTTP.SET_BODY_CHARSET(r=> resp, charset=>'UTF-8');

Place this call directly after Get_response.

Comments

0

Sometimes the problem is not with Unicode. You can use the conversion to ASCII code

 v_param := REPLACE (ASCIISTR (v_param), '\', '\u');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.