この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。
はじめに
データアナリティクス事業本部のkobayashiです。
REST API構築ツール CData API Server で作成したAPIにリクエストを送ると特に設定を行わければJSONフォーマットでレスポンスが返ってきますが、他のフォーマットのレスポンスを受け取ることも簡単な設定を行うだけで可能です。 今回はCData API Serverからの様々なフォーマットのレスポンスを確認してみたいと思います。
CData API Serverのインストール方法やデータソースの設定方法は下記のエントリをご参照ください。
環境
- Amazon Linux 2
- CData API Server - 19.0.7493.0
- RDS MariaDB 10.2.21
また今回使用しているCData API Serverのホストとポートは以下になります。
localhost:8080
APIで使用するソースデータ
今回使うデータソースはMySQLの公式ページからダウンロードできるサンプルの中のworld database
を使います。
中身をデータベースに登録しER図を確認すると以下のようなデータ構造となります。
後に行う追加フォーマットのレスポンスのテストでリレーションも使いますので簡単に内容を確認しておきます。
この図のデータを元にCData API Serverのリソース設定行い、CData API ServerでAPIを構築しレスポンスを受け取ってみます。
CData API Serverの追加フォーマット
CData API Serverのレスポンスは以下のフォーマットのレスポンスを扱えます。
- JSON
- OData(XML)
- JSONP
- HTML
- CSV
- TSV
詳しい内容はCData API Serverの公式ヘルプ に記載があり、以下のような内容となっています。手順は非常に簡単でクライアントにてリクエストを送る際にクエリパラメータとしてフォーマットを指定すれば良いだけです。ただし、フォーマットによってクエリパラメータの形式が異なるのでその点はご留意ください。
では早速、様々なフォーマットでレスポンスを受け取ってみたいと思います。
URL1
city
テーブルのデータをJSONで受け取る(デフォルトの形式)
http://localhost:8080/api.rsc/city/
レスポンス1
JSONフォーマットでレスポンスが取得できます
{
"@odata.context": "http://localhost:8080/api.rsc/$metadata#city",
"value": [
{
"ID": 1,
"Name": "Kabul",
"CountryCode": "AFG",
"District": "Kabol",
"Population": 1780000
},
{
"ID": 2,
"Name": "Qandahar",
"CountryCode": "AFG",
"District": "Qandahar",
"Population": 237500
},
{
"ID": 3,
"Name": "Herat",
"CountryCode": "AFG",
"District": "Herat",
"Population": 186800
}
]
}
URL2
city
テーブルのデータをXMLで受け取る
http://localhost:8080/api.rsc/city/?$format=Atom
レスポンス2
XMLフォーマットでレスポンスが取得できます
<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns:d="http://docs.oasis-open.org/odata/ns/data" xmlns="http://www.w3.org/2005/Atom" xmlns:m="http://docs.oasis-open.org/odata/ns/metadata" xmlns:info="http://www.rssbus.com/ns?RsbOps/v2/" xml:base="http://localhost:8080/api.rsc/">
<title type="text">city</title>
<id>http://localhost:8080/api.rsc/city/</id>
<updated>2020-10-03T07:31:46Z</updated>
<link rel="self" title="city" href="city" />
<entry>
<title>1</title>
<id>http://localhost:8080/api.rsc/city(1)</id>
<category term="CData.city" scheme="http://docs.oasis-open.org/odata/ns/scheme"/>
<link rel="edit" title="city" href="city(1)"/>
<content type="application/xml">
<m:properties>
<d:ID m:type="Int32">1</d:ID>
<d:Name m:type="String">Kabul</d:Name>
<d:CountryCode m:type="String">AFG</d:CountryCode>
<d:District m:type="String">Kabol</d:District>
<d:Population m:type="Int32">1780000</d:Population>
</m:properties>
</content>
</entry>
<entry>
<title>2</title>
<id>http://localhost:8080/api.rsc/city(2)</id>
<category term="CData.city" scheme="http://docs.oasis-open.org/odata/ns/scheme"/>
<link rel="edit" title="city" href="city(2)"/>
<content type="application/xml">
<m:properties>
<d:ID m:type="Int32">2</d:ID>
<d:Name m:type="String">Qandahar</d:Name>
<d:CountryCode m:type="String">AFG</d:CountryCode>
<d:District m:type="String">Qandahar</d:District>
<d:Population m:type="Int32">237500</d:Population>
</m:properties>
</content>
</entry>
<entry>
<title>3</title>
<id>http://localhost:8080/api.rsc/city(3)</id>
<category term="CData.city" scheme="http://docs.oasis-open.org/odata/ns/scheme"/>
<link rel="edit" title="city" href="city(3)"/>
<content type="application/xml">
<m:properties>
<d:ID m:type="Int32">3</d:ID>
<d:Name m:type="String">Herat</d:Name>
<d:CountryCode m:type="String">AFG</d:CountryCode>
<d:District m:type="String">Herat</d:District>
<d:Population m:type="Int32">186800</d:Population>
</m:properties>
</content>
</entry>
</feed>
URL3
city
テーブルのデータをJSONPで受け取る
http://localhost:8080/api.rsc/city/?$callback=MyCallback
レスポンス3
JSONPフォーマットでレスポンスが取得できます
MyCallback({
"@odata.context": "http://localhost:8080/api.rsc/$metadata#city",
"value": [
{
"ID": 1,
"Name": "Kabul",
"CountryCode": "AFG",
"District": "Kabol",
"Population": 1780000
},
{
"ID": 2,
"Name": "Qandahar",
"CountryCode": "AFG",
"District": "Qandahar",
"Population": 237500
},
{
"ID": 3,
"Name": "Herat",
"CountryCode": "AFG",
"District": "Herat",
"Population": 186800
}
]
});
URL4
city
テーブルのデータをHTMLで受け取る
http://localhost:8080/api.rsc/city/?@HTML
レスポンス4
HTMLフォーマットでレスポンスが取得できます
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf8"/>
<style>table, td, th {
font-family: verdana, arial;
font-size: .85em;
padding: 3;
border: 1px solid gainsboro;
border-collapse: collapse
} </style>
</head>
<body>
<table>
<tr>
<th>Population</th>
<th>Name</th>
<th>CountryCode</th>
<th>District</th>
<th>ID</th>
</tr>
<tr>
<td>1780000</td>
<td>Kabul</td>
<td>AFG</td>
<td>Kabol</td>
<td>1</td>
</tr>
<tr>
<td>237500</td>
<td>Qandahar</td>
<td>AFG</td>
<td>Qandahar</td>
<td>2</td>
</tr>
<tr>
<td>186800</td>
<td>Herat</td>
<td>AFG</td>
<td>Herat</td>
<td>3</td>
</tr>
</table>
</body>
</html>
URL5
city
テーブルのデータをCSVで受け取る
http://localhost:8080/api.rsc/city/?@CSV
レスポンス4
CSVフォーマットでレスポンスが取得できます
"Population","Name","CountryCode","District","ID"
"1780000","Kabul","AFG","Kabol","1"
"237500","Qandahar","AFG","Qandahar","2"
"186800","Herat","AFG","Herat","3"
URL6
city
テーブルのデータをTSVで受け取る
http://localhost:8080/api.rsc/city/?@TSV
レスポンス6
TSVフォーマットでレスポンスが取得できます
"Population" "Name" "CountryCode" "District" "ID"
"1780000" "Kabul" "AFG" "Kabol" "1"
"237500" "Qandahar" "AFG" "Qandahar" "2"
"186800" "Herat" "AFG" "Herat" "3"
以上のようなフォーマットでレスポンスを受け取れるので汎用性は非常に高いと思います。
リレーションを使った場合の注意点
CData API Serverの機能の一つである$expand
のパラメータを用いリレーションを使った場合のレスポンスには注意が必要です。リレーション機能を使うとレスポンスは子要素を持った状態になりますので、そのような表現をしにくいフォーマットではリレーションした部分の値が削られて返されます。
CData API Serverのリレーションについては以下の記事をご確認ください。
具体的には以下の分類になります。
- 子要素の値を含む
- JSON
- OData(XML)
- JSONP
- 子要素の値を含まない
- HTML
- CSV
- TSV
ではリレーション機能を使ってレスポンスも確認してみます。
URL1
city
テーブルにcountry
テーブルを結合し、データをJSONで受け取る(デフォルトの形式)
http://localhost:8080/api.rsc/city/?$expand=Country
レスポンス1
JSONフォーマットでレスポンスが取得できます。また子要素もネストされて返されています。
{
"@odata.context": "http://localhost:8080/api.rsc/$metadata#city",
"value": [
{
"Country": {
"Code": "AFG",
"Name": "Afghanistan",
"Continent": "Asia",
"Region": "Southern and Central Asia",
"SurfaceArea": 652090,
"IndepYear": 1919,
"Population": 22720000,
"LifeExpectancy": 45.9,
"GNP": 5976,
"LocalName": "Afganistan/Afqanestan",
"GovernmentForm": "Islamic Emirate",
"HeadOfState": "Mohammad Omar",
"Capital": 1,
"Code2": "AF",
"GNPOld": null
},
"ID": 1,
"Name": "Kabul",
"CountryCode": "AFG",
"District": "Kabol",
"Population": 1780000
},
{
"Country": {
"Code": "AFG",
"Name": "Afghanistan",
"Continent": "Asia",
"Region": "Southern and Central Asia",
"SurfaceArea": 652090,
"IndepYear": 1919,
"Population": 22720000,
"LifeExpectancy": 45.9,
"GNP": 5976,
"LocalName": "Afganistan/Afqanestan",
"GovernmentForm": "Islamic Emirate",
"HeadOfState": "Mohammad Omar",
"Capital": 1,
"Code2": "AF",
"GNPOld": null
},
"ID": 2,
"Name": "Qandahar",
"CountryCode": "AFG",
"District": "Qandahar",
"Population": 237500
},
{
"Country": {
"Code": "AFG",
"Name": "Afghanistan",
"Continent": "Asia",
"Region": "Southern and Central Asia",
"SurfaceArea": 652090,
"IndepYear": 1919,
"Population": 22720000,
"LifeExpectancy": 45.9,
"GNP": 5976,
"LocalName": "Afganistan/Afqanestan",
"GovernmentForm": "Islamic Emirate",
"HeadOfState": "Mohammad Omar",
"Capital": 1,
"Code2": "AF",
"GNPOld": null
},
"ID": 3,
"Name": "Herat",
"CountryCode": "AFG",
"District": "Herat",
"Population": 186800
}
]
}
URL2
city
テーブルにcountry
テーブルを結合し、XMLで受け取る
http://localhost:8080/api.rsc/city/?$expand=Country&$format=Ato
レスポンス2
XMLフォーマットでレスポンスが取得できます。また子要素もネストされて返されています。
<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns:d="http://docs.oasis-open.org/odata/ns/data" xmlns="http://www.w3.org/2005/Atom" xmlns:m="http://docs.oasis-open.org/odata/ns/metadata" xmlns:info="http://www.rssbus.com/ns?RsbOps/v2/" xml:base="http://localhost:8080/api.rsc/">
<title type="text">city</title>
<id>http://localhost:8080/api.rsc/city/</id>
<updated>2020-10-03T07:56:59Z</updated>
<link rel="self" title="city" href="city" />
<entry>
<title>1</title>
<id>http://localhost:8080/api.rsc/city(1)</id>
<category term="CData.city" scheme="http://docs.oasis-open.org/odata/ns/scheme"/>
<link rel="edit" title="city" href="city(1)"/>
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Country" type="application/atom+xml;type=feed" title="Country" href="city(1)/Country">
<inline>
<feed>
<id>http://localhost:8080/api.rsc/city(1)/Country</id>
<title>Country</title>
<updated>2020-10-03T07:56:59Z</updated>
<link rel="self" title="Country" href="city(1)/country"/>
<entry>
<id>http://localhost:8080/api.rsc/Country('AFG')</id>
<title type="text">AFG</title>
<updated>2020-10-03T07:56:59Z</updated>
<link rel="edit" title="Country" href="country('AFG')"/>
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/city" type="application/atom+xml;type=entry" title="city" href="country('AFG')/city"/>
<category term="CData.Country" scheme="http://docs.oasis-open.org/odata/ns/scheme"/>
<content type="application/xml">
<m:properties>
<d:Code m:type="String">AFG</d:Code>
<d:Name m:type="String">Afghanistan</d:Name>
<d:Continent m:type="String">Asia</d:Continent>
<d:Region m:type="String">Southern and Central Asia</d:Region>
<d:SurfaceArea m:type="Decimal">652090</d:SurfaceArea>
<d:IndepYear m:type="Int32">1919</d:IndepYear>
<d:Population m:type="Int32">22720000</d:Population>
<d:LifeExpectancy m:type="Decimal">45.9</d:LifeExpectancy>
<d:GNP m:type="Decimal">5976</d:GNP>
<d:LocalName m:type="String">Afganistan/Afqanestan</d:LocalName>
<d:GovernmentForm m:type="String">Islamic Emirate</d:GovernmentForm>
<d:HeadOfState m:type="String">Mohammad Omar</d:HeadOfState>
<d:Capital m:type="Int32">1</d:Capital>
<d:Code2 m:type="String">AF</d:Code2>
<d:GNPOld m:type="Decimal" m:null="true"/>
</m:properties>
</content>
</entry>
</feed>
</inline>
</link>
<content type="application/xml">
<m:properties>
<d:ID m:type="Int32">1</d:ID>
<d:Name m:type="String">Kabul</d:Name>
<d:CountryCode m:type="String">AFG</d:CountryCode>
<d:District m:type="String">Kabol</d:District>
<d:Population m:type="Int32">1780000</d:Population>
</m:properties>
</content>
</entry>
<entry>
<title>2</title>
<id>http://localhost:8080/api.rsc/city(2)</id>
<category term="CData.city" scheme="http://docs.oasis-open.org/odata/ns/scheme"/>
<link rel="edit" title="city" href="city(2)"/>
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Country" type="application/atom+xml;type=feed" title="Country" href="city(2)/Country">
<inline>
<feed>
<id>http://localhost:8080/api.rsc/city(2)/Country</id>
<title>Country</title>
<updated>2020-10-03T07:56:59Z</updated>
<link rel="self" title="Country" href="city(2)/country"/>
<entry>
<id>http://localhost:8080/api.rsc/Country('AFG')</id>
<title type="text">AFG</title>
<updated>2020-10-03T07:56:59Z</updated>
<link rel="edit" title="Country" href="country('AFG')"/>
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/city" type="application/atom+xml;type=entry" title="city" href="country('AFG')/city"/>
<category term="CData.Country" scheme="http://docs.oasis-open.org/odata/ns/scheme"/>
<content type="application/xml">
<m:properties>
<d:Code m:type="String">AFG</d:Code>
<d:Name m:type="String">Afghanistan</d:Name>
<d:Continent m:type="String">Asia</d:Continent>
<d:Region m:type="String">Southern and Central Asia</d:Region>
<d:SurfaceArea m:type="Decimal">652090</d:SurfaceArea>
<d:IndepYear m:type="Int32">1919</d:IndepYear>
<d:Population m:type="Int32">22720000</d:Population>
<d:LifeExpectancy m:type="Decimal">45.9</d:LifeExpectancy>
<d:GNP m:type="Decimal">5976</d:GNP>
<d:LocalName m:type="String">Afganistan/Afqanestan</d:LocalName>
<d:GovernmentForm m:type="String">Islamic Emirate</d:GovernmentForm>
<d:HeadOfState m:type="String">Mohammad Omar</d:HeadOfState>
<d:Capital m:type="Int32">1</d:Capital>
<d:Code2 m:type="String">AF</d:Code2>
<d:GNPOld m:type="Decimal" m:null="true"/>
</m:properties>
</content>
</entry>
</feed>
</inline>
</link>
<content type="application/xml">
<m:properties>
<d:ID m:type="Int32">2</d:ID>
<d:Name m:type="String">Qandahar</d:Name>
<d:CountryCode m:type="String">AFG</d:CountryCode>
<d:District m:type="String">Qandahar</d:District>
<d:Population m:type="Int32">237500</d:Population>
</m:properties>
</content>
</entry>
<entry>
<title>3</title>
<id>http://localhost:8080/api.rsc/city(3)</id>
<category term="CData.city" scheme="http://docs.oasis-open.org/odata/ns/scheme"/>
<link rel="edit" title="city" href="city(3)"/>
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Country" type="application/atom+xml;type=feed" title="Country" href="city(3)/Country">
<inline>
<feed>
<id>http://localhost:8080/api.rsc/city(3)/Country</id>
<title>Country</title>
<updated>2020-10-03T07:56:59Z</updated>
<link rel="self" title="Country" href="city(3)/country"/>
<entry>
<id>http://localhost:8080/api.rsc/Country('AFG')</id>
<title type="text">AFG</title>
<updated>2020-10-03T07:56:59Z</updated>
<link rel="edit" title="Country" href="country('AFG')"/>
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/city" type="application/atom+xml;type=entry" title="city" href="country('AFG')/city"/>
<category term="CData.Country" scheme="http://docs.oasis-open.org/odata/ns/scheme"/>
<content type="application/xml">
<m:properties>
<d:Code m:type="String">AFG</d:Code>
<d:Name m:type="String">Afghanistan</d:Name>
<d:Continent m:type="String">Asia</d:Continent>
<d:Region m:type="String">Southern and Central Asia</d:Region>
<d:SurfaceArea m:type="Decimal">652090</d:SurfaceArea>
<d:IndepYear m:type="Int32">1919</d:IndepYear>
<d:Population m:type="Int32">22720000</d:Population>
<d:LifeExpectancy m:type="Decimal">45.9</d:LifeExpectancy>
<d:GNP m:type="Decimal">5976</d:GNP>
<d:LocalName m:type="String">Afganistan/Afqanestan</d:LocalName>
<d:GovernmentForm m:type="String">Islamic Emirate</d:GovernmentForm>
<d:HeadOfState m:type="String">Mohammad Omar</d:HeadOfState>
<d:Capital m:type="Int32">1</d:Capital>
<d:Code2 m:type="String">AF</d:Code2>
<d:GNPOld m:type="Decimal" m:null="true"/>
</m:properties>
</content>
</entry>
</feed>
</inline>
</link>
<content type="application/xml">
<m:properties>
<d:ID m:type="Int32">3</d:ID>
<d:Name m:type="String">Herat</d:Name>
<d:CountryCode m:type="String">AFG</d:CountryCode>
<d:District m:type="String">Herat</d:District>
<d:Population m:type="Int32">186800</d:Population>
</m:properties>
</content>
</entry>
</feed>
URL3
city
テーブルにcountry
テーブルを結合し、JSONPで受け取る
http://localhost:8080/api.rsc/city/?$expand=Country&$callback=MyCallback
レスポンス3
JSONPフォーマットでレスポンスが取得できます。また子要素もネストされて返されています。
MyCallback({
"@odata.context": "http://localhost:8080/api.rsc/$metadata#city",
"value": [
{
"Country": {
"Code": "AFG",
"Name": "Afghanistan",
"Continent": "Asia",
"Region": "Southern and Central Asia",
"SurfaceArea": 652090,
"IndepYear": 1919,
"Population": 22720000,
"LifeExpectancy": 45.9,
"GNP": 5976,
"LocalName": "Afganistan/Afqanestan",
"GovernmentForm": "Islamic Emirate",
"HeadOfState": "Mohammad Omar",
"Capital": 1,
"Code2": "AF",
"GNPOld": null
},
"ID": 1,
"Name": "Kabul",
"CountryCode": "AFG",
"District": "Kabol",
"Population": 1780000
},
{
"Country": {
"Code": "AFG",
"Name": "Afghanistan",
"Continent": "Asia",
"Region": "Southern and Central Asia",
"SurfaceArea": 652090,
"IndepYear": 1919,
"Population": 22720000,
"LifeExpectancy": 45.9,
"GNP": 5976,
"LocalName": "Afganistan/Afqanestan",
"GovernmentForm": "Islamic Emirate",
"HeadOfState": "Mohammad Omar",
"Capital": 1,
"Code2": "AF",
"GNPOld": null
},
"ID": 2,
"Name": "Qandahar",
"CountryCode": "AFG",
"District": "Qandahar",
"Population": 237500
},
{
"Country": {
"Code": "AFG",
"Name": "Afghanistan",
"Continent": "Asia",
"Region": "Southern and Central Asia",
"SurfaceArea": 652090,
"IndepYear": 1919,
"Population": 22720000,
"LifeExpectancy": 45.9,
"GNP": 5976,
"LocalName": "Afganistan/Afqanestan",
"GovernmentForm": "Islamic Emirate",
"HeadOfState": "Mohammad Omar",
"Capital": 1,
"Code2": "AF",
"GNPOld": null
},
"ID": 3,
"Name": "Herat",
"CountryCode": "AFG",
"District": "Herat",
"Population": 186800
}
]
});
URL4
city
テーブルにcountry
テーブルを結合し、HTMLで受け取る
http://localhost:8080/api.rsc/city/?$expand=Country&@HTML
レスポンス4
HTMLフォーマットでレスポンスが取得できます。また子要素は含まれていません。
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf8"/>
<style>table, td, th {
font-family: verdana, arial;
font-size: .85em;
padding: 3;
border: 1px solid gainsboro;
border-collapse: collapse
} </style>
</head>
<body>
<table>
<tr>
<th>Population</th>
<th>Name</th>
<th>CountryCode</th>
<th>District</th>
<th>ID</th>
</tr>
<tr>
<td>1780000</td>
<td>Kabul</td>
<td>AFG</td>
<td>Kabol</td>
<td>1</td>
</tr>
<tr>
<td>237500</td>
<td>Qandahar</td>
<td>AFG</td>
<td>Qandahar</td>
<td>2</td>
</tr>
<tr>
<td>186800</td>
<td>Herat</td>
<td>AFG</td>
<td>Herat</td>
<td>3</td>
</tr>
</table>
</body>
</html>
URL5
city
テーブルにcountry
テーブルを結合し、CSVで受け取る
http://localhost:8080/api.rsc/city/?$expand=Country&@CSV
レスポンス4
CSVフォーマットでレスポンスが取得できます。また子要素は含まれていません。
"Population","Name","CountryCode","District","ID"
"1780000","Kabul","AFG","Kabol","1"
"237500","Qandahar","AFG","Qandahar","2"
"186800","Herat","AFG","Herat","3"
URL6
city
テーブルにcountry
テーブルを結合し、TSVで受け取る
http://localhost:8080/api.rsc/city/?$expand=Country&@TSV
レスポンス6
TSVフォーマットでレスポンスが取得できます。また子要素は含まれていません。
"Population" "Name" "CountryCode" "District" "ID"
"1780000" "Kabul" "AFG" "Kabol" "1"
"237500" "Qandahar" "AFG" "Qandahar" "2"
"186800" "Herat" "AFG" "Herat" "3"
まとめ
CData API Serverのレスポンスを様々な形式で受け取ってみました。クエリパラメータに受け取りたい形式を指定するだけで簡単にフォーマットを変更することができとても便利です。
一点、リレーションを使った場合は子要素を受け取れる形式と受け取れない形式がありますので注意してください。
最後まで読んで頂いてありがとうございました。