2

Have a problem with Magento2 rest APIs. I try to import some data to DB table im my module. I wrote rest api, that gets a JSON array and insert it in database table, but when i try to send this array

cities.json:

 {
        "cities":
                    [
                        {
                            "id":7,
                            "name":"Kyiv",
                            "ref":"a9280688-94c0-11e3-b441-0050568002cf",
                            "area_ref":"7150813b-9b87-11de-822f000c2965ae0e"
                        },
                        {
                            "id":10,
                            "name":"Moscow",
                            "ref":"96b8d7a4-be42-11e3-9fa0-0050568002cf",
                            "area_ref":"7150813d-9b87-11de-822f000c2965ae0e"
                        },
                        {
                            "id":11,
                            "name":"London",
                            "ref":"f7062316-4078-11de-b509-001d92f78698",
                            "area_ref":"7150812b-9b87-11de-822f000c2965ae0e"
                        }
                    ]
    }

i've got an error:

{"message":"Internal Error. Details are available in Magento log file. Report ID: webapi-580e2fd764b10"}

in log file: exception.log

Next Exception: Report ID: webapi-580e2fd764b10; Message: Notice: Array to string conversion in /home/workuser/Projects/magentov/vendor/magento/framework/Reflection/TypeProcessor.php on line 505 in /home/workuser/Projects/magentov/vendor/magento/framework/Webapi/ErrorProcessor.php:195 Stack trace: /home/workuser/Projects/magentov/vendor/magento/framework/Webapi/ErrorProcessor.php(139): Magento\Framework\Webapi\ErrorProcessor->_critical(Object(Exception)) /home/workuser/Projects/magentov/vendor/magento/module-webapi/Controller/Rest.php(219): Magento\Framework\Webapi\ErrorProcessor->maskException(Object(Exception)) /home/workuser/Projects/magentov/var/generation/Magento/Webapi/Controller/Rest/Interceptor.php(37): Magento\Webapi\Controller\Rest->dispatch(Object(Magento\Framework\App\Request\Http)) /home/workuser/Projects/magentov/vendor/magento/framework/App/Http.php(135): Magento\Webapi\Controller\Rest\Interceptor->dispatch(Object(Magento\Framework\App\Request\Http)) /home/workuser/Projects/magentov/vendor/magento/framework/App/Bootstrap.php(258): Magento\Framework\App\Http->launch() /home/workuser/Projects/magentov/pub/index.php(37): Magento\Framework\App\Bootstrap->run(Object(Magento\Framework\App\Http)) {main} [] []

I'me looked into TypeProcessor.php on 505 line. When i disable this if:

if ($value !== null && !settype($value[$key], $arrayItemType)) {
                    throw new SerializationException(
                        new Phrase(
                            'Invalid type for value: "%value". Expected Type: "%type".',
                            ['value' => $value, 'type' => $type]
                        )
                    );
                }

everything work fine. I'll get json by POST, decode it and insert all data into DB table, but how can i do it without changing TypeProcessor.php?

I send JSON by curl:

curl -X POST "http://magentov.dev/index.php/rest/V1/NP/cities" -H "Authorization: Bearer uoj2to52kkn46hmdw0m5gh0oax7uoed9" -H "Content-Type:application/json" -d '@cities.json'

Please, help me.

  • did you get some solution for this @Illia Arefyev .. I am facing the same problem.. – Mohammad Mujassam Nov 9 '16 at 6:24
  • @MohammadMujassam yes. I just used auth bearer. – Illia Arefyev Nov 14 '16 at 14:48
  • @MohammadMujassam i'll add full answer here in a few days. Maybe tommorow. Unfortunately, dont have enough time now. – Illia Arefyev Nov 14 '16 at 14:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.