REST API への POST リクエストの送信
HTTP POST リクエストを使用して、単一または複数の RPC リクエストを REST API に送信します。POST 要求を使用してデバイスを設定できます。
単一の rpc
コマンドの場合、エンドポイントの一般的な形式は次のとおりです。
scheme://device-name:port/rpc/method[@attributes]/params
-
scheme
:http
またはhttps
-
method
: Junos OSrpc
コマンドの名前。method
名は、tag 要素と同じです。詳細については、 Junos XML API Explorer を参照してください。 -
params
: 省略可能なパラメーター値 (name[=value]
)。
要求を認証するには、次のいずれかの方法を使用できます。 netrc
オプションの方が安全性が高いため、使用することをお勧めします。
-
認証ヘッダーに含まれる base64 でエンコードされたユーザー名とパスワードを送信します。
curl -u "username:password" http://device-name:port/rpc/get-interface-information
-
または、 .netrc ファイルを使用して資格情報を保存します。
ユーザーのホーム ディレクトリで .netrc ファイルを作成し、リモート デバイスのホスト名、ユーザー名、およびパスワードを指定します。例えば:
user@host:~$ cat ~/.netrc machine 172.16.2.1 login username password password
ユーザーのみがファイルの読み取りと書き込みのアクセス許可を持っていることを確認します。
user@host:~$ chmod 600 .netrc
要求で、
--netrc
オプションを指定します。例えば:user@host:~$ curl --netrc http://172.16.2.1:3000/rpc/get-interface-information
POST 要求の URI でクエリ文字列として rpc
データを指定するには、POST 本文でクエリ データを送信します。このような場合は、次の同等の cURL 呼び出しに示すように、 Content-Type
を text/plain
または application/xml
として指定できます。
curl --netrc http://device-name:port/rpc/get-interface-information --header "Content-Type: text/plain" -d "interface-name=cbp0" curl --netrc http://device-name:port/rpc/get-interface-information --header "Content-Type: application/xml" -d "<interface-name>cbp0</interface-name>"
1 つまたは複数の RPC コマンドと複数の RPC コマンドの両方で、HTTP Accept ヘッダーを使用して、次のいずれかの Content-Type 値を使用して戻り形式を指定できます。
-
アプリケーション/XML (デフォルト)
-
アプリケーション/JSON
-
テキスト/プレーン
-
テキスト/html
たとえば、次の cURL 呼び出しでは、JSON の出力形式を指定します。
curl --netrc http://device-name:port/rpc -d "<get-software-information/>" --header "Accept: application/json"
オプションの format
属性を使用して出力形式を指定することもできます。
curl --netrc http://device-name:port/rpc -d "<get-software-information format='json'/>"
本文に引数を含む POST 要求の既定のコンテンツ タイプは application/xml です。クエリ文字列など、その他のコンテンツを使用する場合は、テキスト/プレーンのコンテンツタイプを指定できます。コンフィグレーションコマンドで format
属性を指定します。
1 つの要求で複数の rpc
コマンドを実行する場合、エンドポイントの一般的な形式は次のとおりです。
scheme://device-name:port/rpc
RPC は、POST 本文で XML データとして指定する必要があります。応答の Content-Type はマルチパート/混合で、境界とサブタイプは各 RPC 実行からの出力に関連付けられています。Accept ヘッダーで指定された形式は、 format
属性がない場合の各 RPC の出力形式として使用されます。特定の RPC で Accept ヘッダーまたは format
属性を指定しない場合、既定の出力形式は XML です。
たとえば、RPC get-software-information
とget-interface-information
を実行する単一の HTTP 要求を送信するには、"Auth: Basic <base64hash>"
、"Content-Type: application/xml"
を使用して /rpc
に POST 要求を送信します。POST 本文には次のものが含まれます。
<get-software-information/><get-interface-information/>
この POST 本文を使用した cURL 呼び出しを次に示します。
curl --netrc 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 本文の各要素の出力形式を指定することもできます。たとえば、次のリクエストは、 get-interface-information
RPC の場合は JSON を出力し、 get-software-information
RPC の場合はプレーンテキストを出力します。
curl --netrc http://device-name:port/rpc -d "<get-interface-information/><get-software-information format='text'/>" --header "Accept: application/json"
複数のRPCを実行する場合、エラーが発生した場合、デフォルトの動作はエラーを無視して実行を続行します。最初のエラーが発生したときに終了する場合は、URI に stop-on-error
フラグを指定します。たとえば、次のリクエストはデバイスを設定し、エラーが発生すると終了します。
curl --netrc 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/>"