簡體   English   中英

POST 請求在 APEX_WEB_SERVICE 調用中返回錯誤的請求響應

[英]POST request returning bad request response in APEX_WEB_SERVICE call

我正在測試來自 reqres.in 站點的 GET 和 POST 請求,以熟悉 APEX_WEB_SERVICE API。 在測試 POST 方法期間,我收到與預期結果不匹配的響應。 我是否以正確的方式發送參數?

示例請求和響應如下所示

**Request**

    {
        "name": "morpheus",
        "job": "leader"
    }

**Response**


    {
        "name": "morpheus",
        "job": "leader",
        "id": "517",
        "createdAt": "2019-10-28T15:46:26.025Z"
    }

我用來調用 web 服務的代碼。

DECLARE
  l_response CLOB;
  l_url VARCHAR2(250) := 'https://reqres.in/api/users';
BEGIN
  --apex_web_service.g_request_headers(1).name := 'Content-Type';
  --apex_web_service.g_request_headers(1).value := 'application/json';

  l_response:=apex_web_service.make_rest_request
    ( p_url => l_url,
      p_http_method => 'POST',
      p_wallet_path => 'file:C:\app\db_home\bin\ow\wallets', --neded for https acccess
      p_wallet_pwd => 'walletPass',
      p_parm_name => apex_util.string_to_table('name:job'),
      p_parm_value => apex_util.string_to_table('"morpheus":"leader"')
    );

  DBMS_OUTPUT.PUT_LINE(l_response);

END;

我收到了回復,但與顯示的預期回復不符。

 - Response I am getting



     {
            "id": "547",
            "createdAt": "2019-10-28T15:45:45.333Z"
        }

 - Response shown at reqres.in



     {
            "name": "morpheus",
            "job": "leader",
            "id": "517",
            "createdAt": "2019-10-28T15:46:26.025Z"
        }

更新:當我使用 Postman 測試 API 時,當我在正文部分設置參數時,我得到了預期的響應。 請看下面

在此處輸入圖像描述

事實證明,我以錯誤的方式傳遞了屍體。 下面的代碼可以正常工作並按預期發布響應。

DECLARE
l_response CLOB;
l_url VARCHAR2(250) := 'https://reqres.in/api/users';
usrid NUMBER;
l_request CLOB;
l_ref_cursor SYS_REFCURSOR;
--cursor c_get_request is
--select 'morpheus' as "name",
--       'leader' as "job"
--from dual;

BEGIN
apex_web_service.g_request_headers(1).name := 'Content-Type';
apex_web_service.g_request_headers(1).value := 'application/json';
--open l_ref_cursor for
--select 'morpheus' as "name",
--       'leader' as "job"
--from dual;
APEX_JSON.initialize_clob_output;
APEX_JSON.open_object; ---{
APEX_JSON.write('name','morpheus');
APEX_JSON.write('job','leader');
APEX_JSON.close_object; --}
l_request := APEX_JSON.get_clob_output;
DBMS_OUTPUT.PUT_LINE(l_request);
l_response :=apex_web_service.make_rest_request(p_url => l_url,
                                               p_http_method => 'POST',
                                               p_wallet_path => 'file:C:\app\db_home\bin\ow\wallets', --neded for https acccess
                                               p_wallet_pwd => 'walletpassword',
                                               p_body => l_request
--                                               p_parm_name => apex_util.string_to_table('name:job'),
--                                               p_parm_value => apex_util.string_to_table('morpheus:leader')
                                               );

DBMS_OUTPUT.PUT_LINE(l_response);
--parse the response
--APEX_JSON.parse(l_response);
--usrid := APEX_JSON.get_number(p_path => 'id');

DBMS_OUTPUT.PUT_LINE('Created ID is :'||usrid);

END;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM