Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?

 
 

Submitting a POST Request to the REST API

Use an HTTP POST request to send single or multiple RPC requests to the REST API. You can use the POST request to configure the device.

For a single rpc command, the general format of the endpoints is:

scheme://device-name:port/rpc/method[@attributes]/params

  • scheme: http or https

  • method: The name of any Junos OS rpc command. The method name is identical to the tag element. For more information, see the Junos XML API Explorer.

  • params: Optional parameter values (name[=value]).

To authenticate your request, you can use one of the following methods. We recommend using the netrc option because it is more secure.

  • Submit the base64-encoded username and password included in the Authorization header.

  • Alternatively, use a .netrc file to store the credentials.

    1. In the user's home directory, create the .netrc file, and specify the hostname, username, and password for the remote device. For example:

    2. Ensure that only the user has read and write permission for the file.

    3. In the request, specify the --netrc option. For example:

To specify rpc data as a query string in the URI for POST requests, submit the query data in the POST body. In such cases you can specify the Content-Type as text/plain or application/xml, as shown in these equivalent cURL calls:

For both single and multiple RPC commands, HTTP Accept headers can be used to specify the return format using one of the following Content-Type values:

  • application/xml (the default)

  • application/json

  • text/plain

  • text/html

For example, the following cURL call specifies an output format of JSON:

You can also specify the output format using the optional format attribute:

Note:

The default Content-Type for POST requests containing arguments in the body is application/xml. If you want to use any other content, such as a query string, you can specify a Content-Type of text/plain. Specify the format attribute in configuration commands.

When executing multiple rpc commands in a single request, the general format of the endpoint is:

scheme://device-name:port/rpc

The RPCs must be provided as XML data in the POST body. The Content-Type for the response is multipart/mixed, with boundary and subtype associated with the output from each RPC execution. The format specified in the Accept header is used as the output format for each of the RPCs if they are missing a format attribute. If you do not specify an Accept header or the format attribute in a given RPC, the default output format is XML.

For example, to send a single HTTP request to execute the RPCs get-software-information and get-interface-information, submit a POST request to /rpc with "Auth: Basic <base64hash>", "Content-Type: application/xml". The POST body would contain:

<get-software-information/><get-interface-information/>

Here is a cURL call using this POST body:

The output from the request, containing XML as the default, would appear as follows:

You can also specify the output format for each of the elements in the POST body. For example, the following request emits JSON for the get-interface-information RPC and plain text for the get-software-information RPC:

When executing multiple RPCs, if an error occurs, the default behavior is to ignore the error and continue execution. If you want to exit when the first error is encountered, specify the stop-on-error flag in the URI. For example, the following request configures the device and terminates if an error is encountered: