curl -i -X POST -u wksp_mcp:[パスワード] https://[ID]-[インスタンス名].adb.[リージョン].oraclecloudapps.com/ords/mcp/_/sql -H "Content-Type: application/sql" --data-binary 'select sysdate from dual'
% curl -i -X POST -u wksp_mcp:********** https://***********-apexdev.adb.us-ashburn-1.oraclecloudapps.com/ords/mcp/_/sql -H "Content-Type: application/sql" --data-binary 'select sysdate from dual'
HTTP/1.1 200 OK
Date: Mon, 02 Dec 2024 09:15:53 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Connection: keep-alive
Strict-Transport-Security: max-age=31536000;includeSubDomains
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
{"env":{"defaultTimeZone":"UTC"},"items":[{"statementId":1,"statementType":"query","statementPos":{"startLine":1,"endLine":2},"statementText":"select sysdate from dual","resultSet":{"metadata":[{"columnName":"SYSDATE","jsonColumnName":"sysdate","columnTypeName":"DATE","columnClassName":"java.sql.Timestamp","precision":7,"scale":0,"isNullable":1}],"items":[{"sysdate":"2024-12-02T09:15:53Z"}],"hasMore":false,"limit":10000,"offset":0,"count":1},"response":[],"result":0}]}% %
select * from eba_country_regions
curlコマンドは以下になります。
% curl -i -X POST -u wksp_mcp:********** https://**************-apexdev.adb.us-ashburn-1.oraclecloudapps.com/ords/mcp/_/sql -H "Content-Type: application/sql" --data-binary 'select * from eba_country_regions'
HTTP/1.1 200 OK
Date: Mon, 02 Dec 2024 09:30:19 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Connection: keep-alive
Strict-Transport-Security: max-age=31536000;includeSubDomains
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
{"env":{"defaultTimeZone":"UTC"},"items":[{"statementId":1,"statementType":"query","statementPos":{"startLine":1,"endLine":2},"statementText":"select * from eba_country_regions","resultSet":{"metadata":[{"columnName":"ID","jsonColumnName":"id","columnTypeName":"NUMBER","columnClassName":"java.math.BigDecimal","precision":0,"scale":-127,"isNullable":0},{"columnName":"NAME","jsonColumnName":"name","columnTypeName":"VARCHAR2","columnClassName":"java.lang.String","precision":255,"scale":0,"isNullable":0}],"items":[{"id":10,"name":"America"},{"id":20,"name":"Europe"},{"id":30,"name":"Asia"},{"id":40,"name":"Oceania"},{"id":50,"name":"Africa"}],"hasMore":false,"limit":10000,"offset":0,"count":5},"response":[],"result":0}]}%
%
{
"env": {
"defaultTimeZone": "UTC"
},
"items": [
{
"statementId": 1,
"statementType": "query",
"statementPos": {
"startLine": 1,
"endLine": 2
},
"statementText": "select * from eba_country_regions",
"resultSet": {
"metadata": [
{
"columnName": "ID",
"jsonColumnName": "id",
"columnTypeName": "NUMBER",
"columnClassName": "java.math.BigDecimal",
"precision": 0,
"scale": -127,
"isNullable": 0
},
{
"columnName": "NAME",
"jsonColumnName": "name",
"columnTypeName": "VARCHAR2",
"columnClassName": "java.lang.String",
"precision": 255,
"scale": 0,
"isNullable": 0
}
],
"items": [
{
"id": 10,
"name": "America"
},
{
"id": 20,
"name": "Europe"
},
{
"id": 30,
"name": "Asia"
},
{
"id": 40,
"name": "Oceania"
},
{
"id": 50,
"name": "Africa"
}
],
"hasMore": false,
"limit": 10000,
"offset": 0,
"count": 5
},
"response": [],
"result": 0
}
]
}
const fetchDataWithBasicAuth = async (url, data, username, password) => {
// Basic認証の資格情報をBase64エンコード
const credentials = btoa(`${username}:${password}`);
try {
const response = await fetch(url, {
method: 'POST',
headers: {
'Authorization': `Basic ${credentials}`,
'Content-Type': 'application/sql',
'Accept': 'application/json'
},
body: data
});
// レスポンスが正常でない場合はエラーをスロー
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
// JSONレスポンスをパース
const jsonData = await response.json();
return jsonData;
} catch (error) {
console.error('リクエストエラー:', error);
throw error;
}
};
// 使用例
const url = 'https://************-apexdev.adb.us-ashburn-1.oraclecloudapps.com/ords/mcp/_/sql';
const postData = 'select sysdate from dual';
// 関数の呼び出し
fetchDataWithBasicAuth(url, postData, 'wksp_mcp', '**********')
.then(data => {
console.log('取得したデータ:', data);
})
.catch(error => {
console.error('エラー発生:', error);
});