Sample code for 30+ languages & platforms
Java

WooCommerce List Products having SKU

See more WooCommerce Examples

Gets information for a product by SKU.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.JsonArray;
import com.chilkatsoft.chilkat2.StringBuilder;

public class ChilkatExample {

    public static void main(String[] args) {
        // This example assumes the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        Http http = new Http();

        // Implements the following CURL command:

        // curl https://example.com/wp-json/wc/v3/products?sku=3386460107914 \
        //     -u consumer_key:consumer_secret

        // Use the following online tool to generate HTTP code from a CURL command
        // Convert a cURL Command to HTTP Source Code

        http.setBasicAuth(true);
        http.setLogin("consumer_key");
        http.setPassword("consumer_secret");

        StringBuilder sbResponseBody = new StringBuilder();
        http.setUrlVar("sku", "3386460107914");
        // Use "https" or "http" depending on what your site needs.
        try {
            http.quickGetSb("http://example.com/wp-json/wc/v3/products?sku={$sku}", sbResponseBody);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        JsonArray jarrResp = new JsonArray();
        jarrResp.loadSb(sbResponseBody);
        jarrResp.setEmitCompact(false);

        System.out.println("Response Body:");
        System.out.println(jarrResp.emit());

        int respStatusCode = http.getLastStatus();
        System.out.println("Response Status Code = " + respStatusCode);
        if (respStatusCode >= 400) {
            System.out.println("Response Header:");
            System.out.println(http.getLastHeader());
            System.out.println("Failed.");
            return;
        }
    }
}