Adding a inventory item using Curl (POST)
For the sake of comparison and contrast, let’s look at adding and inventory item using Curl, instead of the API browser. Make sure you have curl installed, and create the following command line from a command prompt (note that it usually easier to create this command in a text editor like notepad, and copy and paste it to the command line);
Let’s now take the compressed JSON from out previous example, and use it with Curl. There is a catch here though. In order to use the JSON from the command line, the JSON must be “escaped”. This is an issue somewhat unique to using a command line tool like Curl. To escape the string, we make sure that all double quotes (“) and proceeded be a backslash (\). Using a text editor this is easy to do. Simple search and replace the double quote with backslash double quote (\”). The results look like the following;
{\"partNo\":\"GADGET-R\",\"description\":\"A red gadget\",\"pricing\":{\"EA\":{\"sellPrices\":[\"10\",\"9\"]}},\"currentCost\":\"2.00\"}
Now lets use the command line for Curl to send a request to the Spire server to add this inventory item. We will build on this simple example.
curl -i -u RNOTMAN:12345_rsety --data "{\"partNo\":\"GADGET-R\",\"description\":\"A red gadget\",\"pricing\":{\"EA\":{\"sellPrices\":[\"10\",\"9\"]}},\"currentCost\":\"1.00\"}" -H "Content-Type: application/json" https://localhost:10880/api/v2/companies/Testco/inventory/items/ -k --insecure
When you execute this statement you should see a response like the following;
C:\testdata>curl -i -u RNOTMAN:12345_rsety --data "{\"partNo\":\"GADGET-R\",\"description\":\"A red gadget\",\"pricing\":{\"EA\":{\"sellPrices\":[\"10\",\"9\"]}},\"currentCost\":\"1.00\"}" -H "Content-Type: application/json" https://localhost:10880/api/v2/companies/Testco/inventory/items/ -k --insecure HTTP/1.1 201 CREATED Location: https://localhost:10880/api/v2/companies/Testco/inventory/items/3 Content-Type: text/html; charset=utf-8 Content-Length: 0 Date: Thu, 20 Jan 2022 15:40:47 GMT
Now log into Spire, and you should see an inventory item like the following;
Breaking down the command line values
Notice that the command line can be broken down into;
- Authentication. This is the user and password.
- Endpoint. The endpoint or collection you want to update
- HTTP Verb. The action you want to take (POST, PUT, GET, DELETE)
For further examples, lets add one more entry into the command line. We sometimes want to record the results of a command. This is important, because there are times when we want to know if the command was successful, why it failed if it did, and status information if the command was successful. A good example of this is when you have created a sales order, and you want to know the sale order number assigned when the sales order was created by the Spire server.
Let’s create a sales order, and record the response Spire server returns when we do. Here we are working with a company that has a customer RICK, and items WIDGET and GADGET. Here is the command line, where we have also established that we want a text file to be created with the response. The file is called APIRESPONSE.TXT and it’s placed in the C:\Testdata directory.
curl -i -u RNOTMAN:12345_rsety --data "{ \"customer\": {\ "customerNo\": \"ACTTEC\"},\"items\": [{\"partNo\": \"GADGET-R\",\"orderQty\": \"3\"}]}" -H "Content-Type: application/json" https://localhost:10880/api/v2/companies/Testco/sales/orders/ -k --insecure -o C:\Testdata\APIRESPONSE.TXT