What is curl?

curl is really just a tool for transferring data using URLs.  Since a REST API call is URL based, you can use curl to make API calls to your network devices.

How to Install curl

I deployed a CentOS VM from an ISO and curl was preinstalled on that image.  However, if you don’t have curl pre-installed, or need to update it to a more recent version, you can use the package management tool of choice for your Linux OS.  In my case, I will update my curl version using yum.  To install, simply run the command below:

Making your first API call

I’ll walk through the same example of creating a new NSX logical switch that I used in my Getting Started with Postman post.  In this case I will need to make 2 separate API calls.  The first will get the scope ID of my NSX transport zone.  The second will use the scope ID to create the logical switch.  Using the NSX API guide as reference for the URL format.

In the output above you can see I got a certificate error, since I have not imported the self-signed certificate I am using on this NSX manager.  For now I’ll work around that by choosing to ignore certificate errors for this call.  Of course, ignoring the certificate defeats the purpose of having the certificate to begin with, so use this sparingly.  I add the -k option below to ignore the certificate:

You can see the command format above returns the XML reply on a single line, which makes it very difficult for a person to read.  I can clean up the XML by piping it through xmllint.  If xmllint isn’t already installed, you can get it by installing the libxml2 package using your package manager (like yum install libxml2).

From the highlighted line above I can see the object ID of my transport zone is “vdnscope-1”.  Knowing this information, I will now be able to craft my second API call to actually create the logical switch.

Creating the Logical Switch using POST Method

By referencing the NSX API Guide I can see that I need to use the following format for my URL in order to create a new logical switch:

I will need to do 2 things differently from my previous call:

  1. Include a body with my request.
  2. Change the request method to POST.
  3. Include the Content Type header of “application/xml”

In order to include a body, I will create an XML file called “ls_create.xml” following the format above, but containing my configuration data:

Based on the 3 items above, here is the command format I end up with.  You can see the data file is preceded by the “@” symbol.  The response from the NSX manager is simply the object ID of the newly created logical switch – in this case, “virtualwire-27”.

And verifying on the NSX manager, we can see our new logical switch created using curl commands:

 

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

This site uses Akismet to reduce spam. Learn how your comment data is processed.