Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?

Announcement: Try the Ask AI chatbot for answers to your technical questions about Juniper products and solutions.

close
header-navigation
keyboard_arrow_up
close
keyboard_arrow_left
REST API Guide
Table of Contents Expand all
list Table of Contents
file_download PDF
{ "lLangCode": "en", "lName": "English", "lCountryCode": "us", "transcode": "en_US" }
English
keyboard_arrow_right

Submitting a GET Request to the REST API

date_range 27-Sep-24

For an 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.

    content_copy zoom_out_map
    curl -u "username:password" http://device-name:port/rpc/get-interface-information
  • 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:

      content_copy zoom_out_map
      user@host:~$ cat ~/.netrc
      machine 172.16.2.1
      login username
      password password
      
    2. Ensure that only the user has read and write permission for the file.

      content_copy zoom_out_map
      user@host:~$ chmod 600 .netrc
    3. In the request, specify the --netrc option. For example:

      content_copy zoom_out_map
      user@host:~$ curl --netrc http://172.16.2.1:3000/rpc/get-interface-information

To specify rpc data as a query string in the URI for GET requests, you can use a ? following the URI with the & delimiter separating multiple arguments, or use the / delimiter, as shown in these equivalent cURL calls:

For example:

content_copy zoom_out_map
curl --netrc http://device-name:port/rpc/get-interface-information?interface-name=cbp0&snmp-index=1
curl --netrc http://device-name:port/rpc/get-interface-information/interface-name=cbp0/snmp-index=1

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:

content_copy zoom_out_map
curl --netrc http://device-name:port/rpc/get-interface-information?interface-name=cbp0 --header "Accept: application/json"

You can also specify the output format using the Junos OS RPC's optional format parameter.

For example, the <get-software-information> tag element retrieves software process revision levels. The following HTTPS GET request executes this command and retrieves the results in JSON format:

content_copy zoom_out_map
https://device-name:port/rpc/get-software-information@format=json 

The following Python program uses the REST interface to execute the get-route-engine-information RPC, extracts the data from the response, and plots a graph of the CPU load average. The requests module automatically checks the user's .netrc file for credentials associated with the specified device. Alternatively, you can include the auth=(usernamepassword ) argument in the request to supply credentials.

content_copy zoom_out_map
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import requests

def update_line(num, data, line):
    if num == 0:
	    return line,
    global temp_y
    x_data.append(num)
    if num != 0 and num%8 == 1:
        r = requests.get('http://' + device + '/rpc/get-route-engine-information@format=json') 
        if r: temp_y = r.json()['route-engine-information'][0]['route-engine'][0]['load-average-one'][0]['data']
    y_data.append(float(temp_y))
    line.set_data(x_data, y_data)
    return line,

device = input('Enter device:port ')

temp_y = 1
fig1 = plt.figure()
x_data = []
y_data = []
l, = plt.plot([], [])
plt.xlim(0, 80)
plt.ylim(0, 1.5)
plt.xlabel('Time in seconds')
plt.ylabel('CPU utilization (load average)')
plt.title('REST-API test')
line_ani = animation.FuncAnimation(fig1, update_line, 80, fargs=(0, l), interval=1000, blit=True)
plt.show()
footer-navigation