我正在测试来自reqres.in网站的GET和POST请求,以熟悉APEX_WEB_SERVICE API。在测试POST方法期间,我得到的响应与预期结果不匹配。我是否以正确的方式发送参数?
示例请求和响应如下
import React from 'react';
import {
View,
TouchableOpacity,
} from 'react-native';
import MaskedView from '@react-native-community/masked-view';
import LinearGradient from 'react-native-linear-gradient';
export default function () {
return (
<LinearGradient start={{x: 0, y: 0}} end={{x: 1, y: 0}}
colors={['red', 'blue', 'green']}
style={
{flex: 1,
alignItems: 'stretch',
justifyContent: 'center'}
}>
<TouchableOpacity>
<View style={
{height: 100,
alignItems: 'stretch',
justifyContent: 'center',
backgroundColor: 'white',
borderRadius: 30,
//marginLeft: 50, // -> if you uncomment this line, the translucid effect is ruined
}
}>
<MaskedView
style={{height: '100%', backgroundColor: 'yellow',
alignItems: 'stretch', justifyContent: 'center',
}}
maskElement={
<View style={{flex: 1, backgroundColor: 'transparent',
alignItems: 'center', justifyContent: 'center',
}}>
<View style={{width: 300, height: '100%', backgroundColor: 'black'}}/>
</View>
}
>
<LinearGradient start={{x: 0, y: 0}} end={{x: 1, y: 0}}
colors={['red', 'blue', 'green']}
style={{height: '100%'}}
/>
</MaskedView>
</View>
</TouchableOpacity>
</LinearGradient>
);
}
我用来调用Web服务的代码。
**Request**
{
"name": "morpheus",
"job": "leader"
}
**Response**
{
"name": "morpheus",
"job": "leader",
"id": "517",
"createdAt": "2019-10-28T15:46:26.025Z"
}
我收到了答复,但与显示的预期答复不匹配。
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;
更新:当我使用Postman测试API时,在主体部分中设置参数时,得到了预期的响应。请看下面
答案 0 :(得分:0)
事实证明,我以错误的方式传递了身体。下面的代码可以正常工作并按预期发布响应。
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;