Archived Forum Post

Index of archived forum posts

Question:

Create Woocommerce order with Chilkat DLL

Mar 02 '17 at 10:25

I have been working with the Chilkat Chilkat_9_5_0.Rest to connect to woocommerce API.

I can successfully retrieve a list of orders using this line of code -

responseJson = rest.FullRequestNoBody("GET","/wc-api/v3/orders/")

Now I need to be able to place an order using VBScript. Has anyone done this? I believe I need to call the same orders, but I cannot figure out how to sent the parameters I need to place the order. Does anyone have an example of doing this in VB Script with the Chilkat dll?

Thanks in advance!


Answer

Writing the Chilkat REST code to any REST API call should be fairly straightforward. Here's how I go about doing it...

First find the reference documentation for the REST API. In this case, the WooCommerce REST API reference documentation is at: http://woocommerce.github.io/woocommerce-rest-api-docs/#orders

Step 1 -- What is the HTTP Verb?
If just getting information, then it's likely a GET. If sending information, such as for placing an order, updating information, creating something, etc., then it would be a POST or GET. The DELETE HTTP verb would typically be used for deleting something. The service's REST API reference documentation should specify what verb to use. In this particular case, it's a POST.

Step 2 -- What is the Path?
What is the path part of the URI? Looking at the WooCommerce documentation for placing an order, the path is "/wp-json/wc/v1/orders".

Step 3: How is the information sent?
When a POST or PUT is used, the information of the request can be sent in a number of possible ways. Typical options are:

  1. JSON or XML in the request body. In this case, the Content-Type of the HTTP request would typically be application/json or text/xml (or possibly application/xml).
  2. Request Parameters. In this case, the Content-Type is application/x-www-form-urlencoded, and request body is composed of the request parameters in URL encoded format (just like they would be in the URL for a GET request, but instead placed in the body of the request to eliminate size restrictions). Request parameters are added via the Chilkat.Rest.AddQueryParam method.
  3. HTTP Request Headers. In this case, information is sent in the MIME request header (i.e. they are header fields just like Content-Type is a header field). HTTP request headers are added via the Chilkat.Rest.AddHeader method.

It may be that information is sent both in request headers and in the request body. It's also possible that a "GET" request could serve for sending information to the server if the information is contained in the GET request params on the URL, but this is less likely because the information is visible to the world..

Looking at the WooCommerce REST API documentation for placing an order, we can see that the order information is JSON that sent in the body of the request. For cases where the request body contains JSON or XML, the methods of chose are Rest.FullRequestSb or Rest.FullRequestString.

(If the information was passed in query params, then we'd choose the FullRequestFormUrlEncoded method.)

So... to place the WooCommerce order, you would first construct the JSON specifying the order data. Then call Rest.FullRequestString(string httpVerb, string uriPath, string bodyText).

In this case, the httpVerb = "POST".
The uriPath = "/wp-json/wc/v1/orders".
The bodyText contains the JSON for the order.

Don't forget to also set the Content-Type header for the request to "application/json" by calling Rest.AddHeader("Content-Type","application/json")

Step 4 -- The Response Finally, look at the service's REST API documentation (WooCommerce in this case) to see the expected response. What is the expected success response code? Is it 200, 201, 204? What is the format and location of the response information? In this case, the response information is JSON contained in the response body. If the response code is not clearly indicated, then accept either 200 or 201 as success. A 204 response is generally for success where there is no response data.