向 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
与标记元素相同。有关详细信息,请参阅 Junos Junos XML 管理协议开发人员指南 和 Junos XML API 操作开发人员参考中的 Junos XML 协议操作、处理说明和响应 标记。params
:可选参数值 (name[=value]
)。
要认证您的请求,请提交授权标头中包含的 base64 编码的用户名和密码:
curl -u "username:password" http://device-name:port/rpc/get-interface-information
要将数据 rpc
指定为 URI 中用于 POST 请求的查询字符串,请提交 POST 正文中的查询数据。在这种情况下,您可以指定 作为 Content-Type
text/plain
或 application/xml
,如以下等效 cURL 调用所示:
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 是 application/xml。如果要使用任何其他内容(如查询字符串),您可以指定 Content-Type of text/plain。在 format
配置命令中指定属性。
在单个 rpc
请求中执行多个命令时,端点的常规格式为:
scheme://device-name:port/rpc
RPC 必须在 POST 正文中作为 XML 数据提供。响应的内容类型是多部分/混合型,与每个 RPC 执行的输出相关联的边界和子类型。如果缺少属性,在"接受"标头中指定的格式将用作每个 RPC 的输出 format
格式。如果未指定接受标头,且给定 RPC 中未指定属性,则默认 format
输出格式为 XML。例如,要发送单个 HTTP 请求以执行 RPC get-software-information
和 ,请将 get-interface-information
POST 请求 /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 的 JSON 和 get-interface-information
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
URI 中的标记。例如,以下请求可配置设备,并终止在遇到错误时终止:
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/>"