POST リクエストの REST API への送信
HTTP POST リクエストを使用して、1 つまたは複数の RPC リクエストを REST API に送信します。POST リクエストを使用してデバイスを設定できます。
単一の rpc
コマンドの場合、エンドポイントの一般的な形式は以下になります。
scheme://device-name:port/rpc/method[@attributes]/params
scheme
:http
またはhttps
method
:任意の Junos OSrpc
コマンドの名前。名前method
はタグ要素と同一です。詳細については、「 Junos XML 管理プロトコル開発者ガイド 」および「 Junos XML API Operational Developer Reference」の「 Junos Junos XMLプロトコル操作、処理手順、応答タグ」を参照してください。params
:オプションのパラメータ値name[=value]
( ) 。
リクエストを認証するには、次の許可ヘッダーに含まれる base64 エンコードのユーザー名とパスワードを送信します。
curl -u "username:password" http://device-name:port/rpc/get-interface-information
POST リクエスト rpc
の場合は、クエリ データを POST リクエストの際に、クエリ文字列として指定するには、クエリー データを POST 本体に送信します。この場合は、次の同等のcURLコールに示すように、 を Content-Type
text/plain
application/xml
指定できます。
curl -u "username:password" http://device-name:port/rpc/get-interface-information --header "Content-Type: text/plain" –d "interface-name=cbp0" curl -u "username:password" http://device-name:port/rpc/get-interface-information --header "Content-Type: application/xml" –d "<interface-name>cbp0</interface-name>"
単一コマンドと複数の RPC コマンドの両方で、HTTP Accept ヘッダーを使用して、以下のいずれかの Content-Type 値を使用して戻り形式を指定できます。
application/xml(デフォルト)
アプリケーション/json
テキスト/プレーン
テキスト/html
たとえば、次のcURLコールは、JSONの出力形式を指定します。
curl -u "username:password" http://device-name:port/rpc -d <get-software-information /> –header "Accept: application/json"
また、オプションの属性を使用して出力形式を指定 format
できます。
curl -u "username:password" http://device-name:port/rpc -d "<get-software-information format=application/json'/>"
本文に引数を含む POST リクエストのデフォルトの Content-Type はアプリケーション/xml です。クエリー文字列など、他のコンテンツを使用する場合は、テキスト/プレーンのコンテンツ タイプを指定できます。設定コマンド format
で属性を指定します。
1 つのリクエスト rpc
で複数のコマンドを実行する場合、エンドポイントの一般的な形式は以下になります。
scheme://device-name:port/rpc
RPC は、POST 本体に XML データとして提供する必要があります。レスポンスのコンテンツ タイプはマルチパート/混合で、各 RPC 実行の出力に関連付けられた境界と大分類です。Accept ヘッダーに指定された形式は、各 RPC に属性が欠落している場合の出力形式として使用 format
されます。Accept ヘッダーが指定されていない場合で、特定の RPC に属性が指定されていない場合、デフォルトの出力形式は format
XML になります。たとえば、1 つの HTTP リクエストを送信して RPC を実行し、 を使用して POST リクエストを get-software-information
get-interface-information
、 /rpc
"Auth: Basic <base64hash>"
. "Content-Type: application/xml"
POST の本文には以下が含まれる:
<get-software-information/><get-interface-information/>
この POST 本文を使用した cURL コールを以下に示します。
curl -u "username:password" http://device-name:port/rpc -d "<get-software-information/><get-interface-information/>"
XML をデフォルトとして含むリクエストからの出力は、次のように表示されます。
HTTP/1.1 200 OK Content-Type: multipart/mixed; boundary=fkj49sn38dcn3 Transfer-Encoding: chunked Date: Thu, 20 Mar 2014 11:01:27 GMT Server: lighttpd/1.4.32 --fkj49sn38dcn3 Content-Type: application/xml <software-information> <host-name>...</host-name> ... </software-information> --fkj49sn38dcn3 Content-Type: application/xml <interface-information> <physical-interface>...</physical-interface> </interface-information> --fkj49sn38dcn3--
POST 本体の各要素に出力形式を指定することもできます。たとえば、次のリクエストは RPC の get-interface-information
JSON、RPC のプレーン テキストを送信 get-software-information
します。
curl -u "username:password" http://device-name:port/rpc -d "<get-interface-information/><get-software-information format='text/plain'/>" —header "Accept: application/json"
複数の RPC を実行する場合、エラーが発生した場合、デフォルトの動作はエラーを無視して実行を継続します。最初のエラーが発生した際に終了する場合は、このフラグ stop-on-error
をこのSEDで指定します。たとえば、次のリクエストではデバイスを設定し、エラーが発生すると終了します。
curl -u "username:password" http://device-name:port/rpc?stop-on-error=1 -d "<lock-configuration/> <load-configuration> <configuration><system><hostname>foo</hostname></system></configuration> </load-configuration> <commit/> <unlock-configuration/>"