# Wie poste ich über REST API einen Artikel?

**URL:** <https://forum.shopware.com/t/wie-poste-ich-uber-rest-api-einen-artikel/60660>\
**Category:** Allgemein\
**Created:** [25. Juli 2019 um 09:50 UTC](https://forum.shopware.com/t/wie-poste-ich-uber-rest-api-einen-artikel/60660 "2019-07-25T09:50:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jugo](https://avatars.discourse-cdn.com/v4/letter/j/ba8739/32.png) [@jugo](https://forum.shopware.com/u/jugo)\
**Post date:** [25. Juli 2019 um 09:50 UTC](https://forum.shopware.com/t/wie-poste-ich-uber-rest-api-einen-artikel/60660/1 "2019-07-25T09:50:13Z")

</div>

Hey, verbringe mittlerweile schon 2 Tage damit, bei einer frischen SW6 Installation die API zum laufen zu bringen.

Wie genau muss ich denn die beiden Dokumente API.php und Call.php nun aufbauen?

Bzgl. der API.php habe ich das Standard Dokument für Tests genommen, jedoch funktioniert es nicht.

Habe mich zunächst erstmal darauf beschränkt über GET überhaut erstmal Werte zu zurückzubekommen, indem fall alle Artikel, doch selbst das geht nicht.

Wo genau liegt der Fehler?

```
apiUrl = rtrim($apiUrl, '/') . '/';
        //Initializes the cURL instance
        $this->cURL = curl_init();
        curl_setopt($this->cURL, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($this->cURL, CURLOPT_FOLLOWLOCATION, false);
        curl_setopt($this->cURL, CURLOPT_USERAGENT, 'Shopware ApiClient');
        curl_setopt($this->cURL, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
        curl_setopt($this->cURL, CURLOPT_USERPWD, $username . ':' . $apiKey);
        curl_setopt(
            $this->cURL,
            CURLOPT_HTTPHEADER,
            ['Content-Type: application/json; charset=utf-8']
        );
    }

    public function call($url, $method = self::METHOD_GET, $data = [], $params = [])
    {
        if (!in_array($method, $this->validMethods)) {
            throw new Exception('Invalid HTTP-Methode: ' . $method);
        }
        $queryString = '';
        if (!empty($params)) {
            $queryString = http_build_query($params);
        }
        $url = rtrim($url, '?') . '?';
        $url = $this->apiUrl . $url . $queryString;
        $dataString = json_encode($data);
        curl_setopt($this->cURL, CURLOPT_URL, $url);
        curl_setopt($this->cURL, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($this->cURL, CURLOPT_POSTFIELDS, $dataString);
        $result = curl_exec($this->cURL);
        $httpCode = curl_getinfo($this->cURL, CURLINFO_HTTP_CODE);

        return $this->prepareResponse($result, $httpCode);
    }

    public function get($url, $params = [])
    {
        return $this->call($url, self::METHOD_GET, [], $params);
    }

    public function post($url, $data = [], $params = [])
    {
        return $this->call($url, self::METHOD_POST, $data, $params);
    }

    public function put($url, $data = [], $params = [])
    {
        return $this->call($url, self::METHOD_PUT, $data, $params);
    }

    public function delete($url, $params = [])
    {
        return $this->call($url, self::METHOD_DELETE, [], $params);
    }

    protected function prepareResponse($result, $httpCode)
    {
        echo "HTTP: $httpCode";
        if (null === $decodedResult = json_decode($result, true)) {
            $jsonErrors = [
                JSON_ERROR_NONE => 'No error occurred',
                JSON_ERROR_DEPTH => 'The maximum stack depth has been reached',
                JSON_ERROR_CTRL_CHAR => 'Control character issue, maybe wrong encoded',
                JSON_ERROR_SYNTAX => 'Syntaxerror',
            ];
            echo 'Could not decode json';
            echo 'json_last_error: ' . $jsonErrors[json_last_error()];
            echo 'Raw:';
            echo '' . print_r($result, true) . '';

            return;
        }
        if (!isset($decodedResult['success'])) {
            echo 'Invalid Response';

            return;
        }
        if (!$decodedResult['success']) {
            echo 'No Success';
            echo '' . $decodedResult['message'] . '';
            if (array_key_exists('errors', $decodedResult) && is_array($decodedResult['errors'])) {
                echo '' . join('', $decodedResult['errors']) . '';
            }

            return;
        }
        echo 'Success';
        if (isset($decodedResult['data'])) {
            echo '' . print_r($decodedResult['data'], true) . '';
        }

        return $decodedResult;
    }
}

```

die call.php ebenso:

```
get('articles');

?>

```

Besten Gruß

Julian

---

<div class="post-metadata">

**Author:** ![Moritz\_Naczenski](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/moritz_naczenski/32/7792_2.png) [@Moritz\_Naczenski](https://forum.shopware.com/u/Moritz_Naczenski)\
**Post date:** [25. Juli 2019 um 10:12 UTC](https://forum.shopware.com/t/wie-poste-ich-uber-rest-api-einen-artikel/60660/2 "2019-07-25T10:12:58Z")

</div>

Hast du den Client selbst geschrieben?  
Sieht sehr nach dem SW5 Client aus.´, der funktioniert in SW6 nicht.

---

<div class="post-metadata">

**Author:** ![jugo](https://avatars.discourse-cdn.com/v4/letter/j/ba8739/32.png) [@jugo](https://forum.shopware.com/u/jugo)\
**Post date:** [25. Juli 2019 um 10:26 UTC](https://forum.shopware.com/t/wie-poste-ich-uber-rest-api-einen-artikel/60660/3 "2019-07-25T10:26:58Z")

</div>

Hey, ja das war der Beispiel Client aus der Doku [REST API](https://developers.shopware.com/developers-guide/rest/).

Was muss ich da anders machen damit er mit SW6 arbeitet?

Besten Gruß

Julian

&nbsp;

---

<div class="post-metadata">

**Author:** ![Moritz\_Naczenski](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/moritz_naczenski/32/7792_2.png) [@Moritz\_Naczenski](https://forum.shopware.com/u/Moritz_Naczenski)\
**Post date:** [25. Juli 2019 um 10:51 UTC](https://forum.shopware.com/t/wie-poste-ich-uber-rest-api-einen-artikel/60660/4 "2019-07-25T10:51:34Z")

</div>

Da haben wir ein anderes Beispiel in der SW6 Doku:&nbsp;[Shopware 6: Working with the Rest-API and an HTTP Client](https://docs.shopware.com/en/shopware-platform-dev-en/how-to/working-with-the-api-and-an-http-client)

---

<div class="post-metadata">

**Author:** ![FloC3](https://avatars.discourse-cdn.com/v4/letter/f/838e76/32.png) [@FloC3](https://forum.shopware.com/u/FloC3)\
**Post date:** [27. Mai 2020 um 18:06 UTC](https://forum.shopware.com/t/wie-poste-ich-uber-rest-api-einen-artikel/60660/5 "2020-05-27T18:06:57Z")

</div>

bei mir funktioniert dieses Beispiel nicht [@Moritz Naczenski]([http://forum.shopware.com/profile/14574/Moritz](http://forum.shopware.com/profile/14574/Moritz) Naczenski “Moritz Naczenski”)‍ …

siehe mein Thread&nbsp;[https://forum.shopware.com/discussion/68890/sw6-api-doku-wo-beispiele#latest](https://forum.shopware.com/discussion/68890/sw6-api-doku-wo-beispiele#latest)

kann sich das jemand ansehen? Bekomme da nie Daten zurück sondern immer nur quasi einen Header …
