テスト用の無料 SOAP API(WSDL 付き)
WSDL 付きの電卓サービスと国のサービス。どちらも SOAP 1.1 と 1.2 で応答します。
例を API に対して最後に実行した日:
クライアントをテストできる公開 SOAP API なら、https://api.testingapis.com/api/soap/calculatorサインインして表示 を使ってください。WSDL は https://api.testingapis.com/api/soap/calculator?wsdlサインインして表示 にあり、SoapUI、Postman、WCF、Python の zeep で読み込めます。操作は Add、Subtract、Multiply、Divide の 4 つです。SOAP 1.1 と SOAP 1.2 を受け付け、正しい SOAP fault を返すので、結果だけでなくエラーもテストできます。もう 1 つのサービス /api/soap/countriesサインインして表示 は構造化された国のレコードを返します。どちらも無料で、登録も API キーも不要です。
| 電卓 | 国 | |
|---|---|---|
| エンドポイント | POST /api/soap/calculatorサインインして表示 | POST /api/soap/countriesサインインして表示 |
| WSDL | GET /api/soap/calculator?wsdlサインインして表示 | なし。エンドポイントに GET すると、使い方の JSON とサンプルのエンベロープが返る |
| 名前空間 | http://tempuri.org/ | http://example.com/countries |
| 操作 | Add、Subtract、Multiply、Divide(整数) | GetCountryInfo(countryCode)、ListCountries() |
| バージョン | SOAP 1.1 と 1.2 | SOAP 1.1 と 1.2 |
curl で呼ぶ(SOAP 1.1)
SOAP 1.1 では Content-Type: text/xml と、操作名を示す SOAPAction ヘッダーを使います。ここではヘッダーは任意ですが、送る場合はボディの操作と一致している必要があります。
curl -X POST https://api.testingapis.com/api/soap/calculator \
-H 'Content-Type: text/xml; charset=utf-8' \
-H 'SOAPAction: "http://tempuri.org/Add"' \
-d '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<Add xmlns="http://tempuri.org/"><intA>5</intA><intB>3</intB></Add>
</soap:Body>
</soap:Envelope>'HTTP/1.1 200 OK
content-type: text/xml; charset=utf-8
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<AddResponse xmlns="http://tempuri.org/">
<AddResult>8</AddResult>
</AddResponse>
</soap:Body>
</soap:Envelope>SOAP 1.2 と fault
SOAP 1.2 では Content-Type: application/soap+xml と別のエンベロープ名前空間を使い、SOAPAction ヘッダーはありません。レスポンスはリクエストと同じバージョンで返ります。0 で割ると Receiver fault が返ります。
curl -X POST https://api.testingapis.com/api/soap/calculator \
-H 'Content-Type: application/soap+xml; charset=utf-8' \
-d '<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
<soap:Body>
<Divide xmlns="http://tempuri.org/"><intA>10</intA><intB>0</intB></Divide>
</soap:Body>
</soap:Envelope>'HTTP/1.1 500 Internal Server Error
content-type: application/soap+xml; charset=utf-8
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
<soap:Body>
<soap:Fault>
<soap:Code>
<soap:Value>soap:Receiver</soap:Value>
</soap:Code>
<soap:Reason>
<soap:Text xml:lang="en">Attempted to divide by zero.</soap:Text>
</soap:Reason>
</soap:Fault>
</soap:Body>
</soap:Envelope>fault のルールは仕様どおりで、クライアントのテストに必要なのはまさにこれです。
| ケース | SOAP 1.1 | SOAP 1.2 |
|---|---|---|
| 不正な入力(未知の国コード) | 500、faultcode soap:Client | 400、Code soap:Sender |
| サーバー側のエラー(0 による除算) | 500、faultcode soap:Server | 500、Code soap:Receiver |
| SOAPAction が別の操作を指している | 500 の fault | 該当なし |
| 誤った Content-Type(JSON など) | JSON のエラー付きの 415 | JSON のエラー付きの 415 |
SOAP クライアントから使う
SoapUI
- File から New SOAP Project を選びます。
- Initial WSDL に
https://api.testingapis.com/api/soap/calculator?wsdlサインインして表示 を貼り付けて完了します。SoapUI が両方のバインディングの各操作にリクエストを作ります。 - リクエストを開き、
intAとintBを入力して実行します。
Python と zeep
zeep は WSDL を読んで型付きのクライアントを作り、SOAP fault は Python の例外になります。
# pip install zeep
from zeep import Client
client = Client("https://api.testingapis.com/api/soap/calculator?wsdl")
print(client.service.Add(intA=5, intB=3))
print(client.service.Multiply(intA=6, intB=7))
try:
client.service.Divide(intA=10, intB=0)
except Exception as err:
print(type(err).__name__, err)8
42
Fault Attempted to divide by zero.JavaScript と fetch
SOAP ライブラリを使わない場合、リクエストは XML の文字列で、結果はレスポンスのテキストから取り出します。
const envelope = `<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<Multiply xmlns="http://tempuri.org/">
<intA>6</intA>
<intB>7</intB>
</Multiply>
</soap:Body>
</soap:Envelope>`;
const res = await fetch("https://api.testingapis.com/api/soap/calculator", {
method: "POST",
headers: {
"Content-Type": "text/xml; charset=utf-8",
SOAPAction: '"http://tempuri.org/Multiply"',
},
body: envelope,
});
const xml = await res.text();
console.log(res.status, xml.match(/<MultiplyResult>(.*?)<\/MultiplyResult>/)[1]);
// 200 42国のサービス
電卓が返すのは数値だけです。ネストしたレコードの解析をテストするなら、国のサービスを使ってください。10 か国(US、GB、IN、DE、FR、 JP、BR、CA、AU、ZA)があり、ListCountries はコードと名前を、GetCountryInfo は 1 件の完全なレコードを返します。
curl -X POST https://api.testingapis.com/api/soap/countries \
-H 'Content-Type: text/xml' \
-d '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<GetCountryInfo xmlns="http://example.com/countries">
<countryCode>IN</countryCode>
</GetCountryInfo>
</soap:Body>
</soap:Envelope>'<GetCountryInfoResponse xmlns="http://example.com/countries">
<Country>
<Code>IN</Code>
<Name>India</Name>
<Capital>New Delhi</Capital>
<Currency>INR</Currency>
<PhoneCode>+91</PhoneCode>
<Continent>Asia</Continent>
<Population>1428627663</Population>
</Country>
</GetCountryInfoResponse>未知のコードはクライアント fault になります。
HTTP/1.1 500 Internal Server Error
<soap:Fault>
<faultcode>soap:Client</faultcode>
<faultstring>Country code 'ZZ' not found.</faultstring>
</soap:Fault>関連する API
SOAP API のリファレンスには、すぐ送れるエンベロープ付きで全操作が載っています。通常の REST で XML を扱うクライアントをテストするなら、XML ユーザー API を試してください。JSON から XML へのコンバーターは、SOAP のペイロードを JSON の同等物と比べるときに役立ちます。WSDL、fault、XPath、Java のテストスイートまで含めて SOAP を基礎から学ぶなら、SOAP と XML のチュートリアル(英語)をどうぞ。