Sample code for 30+ languages & platforms
Java

Shopware Delete Media

See more Shopware Examples

Demonstrates how to delete a media file from a Shopware shop.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.HttpResponse;
import com.chilkatsoft.chilkat2.JsonObject;
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();

        http.setLogin("api_username");
        http.setPassword("api_key");
        http.setBasicAuth(true);

        // The id of the product is appended to the path part of the URL.
        http.setUrlVar("id", "6864");

        String url = "https://my-shopware-shop.com/api/media/{$id}";

        // Send a DELETE request with nothing in the request body.
        HttpResponse resp = new HttpResponse();
        try {
            http.httpNoBody("DELETE", url, resp);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        StringBuilder sbResponseBody = new StringBuilder();
        resp.getBodySb(sbResponseBody);

        JsonObject jResp = new JsonObject();
        jResp.loadSb(sbResponseBody);
        jResp.setEmitCompact(false);

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

        // A 200 response code indicates success (i.e. the request was sent and a response was received).
        int respStatusCode = resp.getStatusCode();
        System.out.println("Response Status Code = " + respStatusCode);
        if (respStatusCode >= 400) {
            System.out.println("Response Header:");
            System.out.println(resp.getHeader());
            System.out.println("Failed.");
            return;
        }

        // Sample JSON response:

        // {
        //   "success": true
        // }

        boolean bDeleted = jResp.boolOf("success");
        System.out.println("Deleted: " + bDeleted);

        // A failed response would look like this:

        // {
        //   "success": false,
        //   "message": "Product by \"id\" 6864 not found"
        // }
    }
}