gpt4 book ai didi

oracle - PL/SQL : UTL_HTTP POST with UTF8 string results in broken characters

转载 作者:可可西里 更新时间:2023-11-01 15:21:44 45 4
gpt4 key购买 nike

我想通过 utl_http 将 UTF8 json 字符串通过 POST 发送到 node.js 服务器。发送字符串

["Sant Julià de Lòria"]

如实到达

["Sant Juli� de L�ria"]

PL/SQL 代码如下:

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;

最佳答案

您应该将代码更改为:

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));

由于 UTF-8,LENGTHB 表示字节长度。否则,计算出的长度将为错误,并且您会在目标端出现错误(输入意外结束 或其他)。

关于oracle - PL/SQL : UTL_HTTP POST with UTF8 string results in broken characters,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21851717/

45 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com