向 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 XML API 资源管理器。 -
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 正文中提交查询数据。在这种情况下, Content-Type
您可以指定 as text/plain
或 application/xml
,如以下等效 cURL 调用所示:
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>"
对于单个和多个 RPC 命令,HTTP 接受标头可用于使用以下内容类型值之一指定返回格式:
-
应用程序/XML(默认值)
-
应用程序/JSON
-
文本/纯文本
-
文本/网页
例如,以下 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
在配置命令中指定属性。
在单个请求中执行多个 rpc
命令时,终结点的一般格式为:
scheme://device-name:port/rpc
RPC 必须作为 XML 数据在 POST 正文中提供。响应的内容类型是多部分/混合的,边界和子类型与每个 RPC 执行的输出相关联。如果每个 RPC 缺少 format
属性,则在“接受”标头中指定的格式用作输出格式。如果未在给定 RPC 中指定“接受”标头或 format
属性,则默认输出格式为 XML。
例如,要发送单个 HTTP 请求来执行 RPC get-software-information
get-interface-information
,然后向 提交 "Auth: Basic <base64hash>"
POST 请求/rpc
,请使用 。 "Content-Type: application/xml"
开机自检正文将包含:
<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 正文中的每个元素指定输出格式。例如,以下请求为 RPC 发出 get-interface-information
JSON 和为 RPC 发出 get-software-information
纯文本:
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/>"