使用apex_web_service.make_rest_request遇500内部错误求助
问题:APEX中HTTPS POST身份验证请求返回500错误(请求头相关)
我需要向某个端点发送HTTPS POST请求完成身份验证,怀疑问题出在请求头设置上。XML请求体已在Postman中验证有效,但用APEX代码发送时返回500内部错误,和Postman中缺少部分请求头时的报错一致。
初始请求头设置代码
-- Set the request headers apex_web_service.g_request_headers(1).name := 'Content-Type'; apex_web_service.g_request_headers(1).value := 'application/xml'; apex_web_service.g_request_headers(2).name := 'x-rs-key'; apex_web_service.g_request_headers(2).value := '<key>'; apex_web_service.g_request_headers(3).name := 'Host'; apex_web_service.g_request_headers(3).value := '<host>';
HTTPS请求执行代码
-- Set the HTTPS request l_http_resp := apex_web_service.make_rest_request( p_url => l_auth_url, p_http_method => 'POST', p_body_blob => l_request_object );
请求体XML内容
<?xml version="1.0" encoding="UTF-8" ?> <AuthenticationCredentials xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> <UserName>{USERNAME}</UserName> <Password>{PASSWORD}</Password> <AuthenticationType>SetCookie</AuthenticationType> <AuthenticationOptions>None</AuthenticationOptions> <AuthenticationChallangeResponse i:nil="true" /> <ClientDeviceDescription i:nil="true" /> </AuthenticationCredentials>
尝试过的另一种请求头设置方式
apex_web_service.set_request_headers( p_name_01 => 'Content-Type', p_value_01 => 'application/json', p_name_02 => 'User-Agent', p_value_02 => 'APEX', p_reset => false, p_skip_if_exists => true );
可能的错误点及修正方案
Content-Type不一致:Postman中验证有效的请求用的是
application/xml,但第二种尝试里错误改成了application/json,服务端无法解析XML请求体直接引发500错误。必须保持和Postman一致的application/xml。冗余的Host头设置:
apex_web_service.make_rest_request会自动根据传入的p_url生成Host头,手动设置可能导致格式冲突。Postman中通常无需手动指定Host头,建议删除代码中手动设置的Host头。请求头残留问题:直接给
g_request_headers数组赋值时,若未清空旧请求头可能导致残留数据干扰。建议设置新头前先调用apex_web_service.clear_request_headers清空历史数据。第二种设置方式的参数错误:
p_reset => false会保留旧请求头,p_skip_if_exists => true可能导致Content-Type未被正确覆盖;同时该方式未设置关键的x-rs-key头,缺少它会触发500错误。
修正后的代码示例
-- 先清空旧请求头,避免残留 apex_web_service.clear_request_headers; -- 设置与Postman一致的请求头 apex_web_service.set_request_headers( p_name_01 => 'Content-Type', p_value_01 => 'application/xml', p_name_02 => 'x-rs-key', p_value_02 => '<your actual key>', p_reset => true, p_skip_if_exists => false ); -- 执行HTTPS请求 l_http_resp := apex_web_service.make_rest_request( p_url => l_auth_url, p_http_method => 'POST', p_body_blob => l_request_object );
内容的提问来源于stack exchange,提问作者Rody
相关产品推荐
相关产品推荐

