how to pass json ecoded array in custom magento2 API
I have created custom API that excepting 'mixed' type parameter and returning the 'string' as I defined. In interface I define parameter type 'mixed'. As I am trying to pass array as json_encode, it giving me Warning: json_decode() expects parameter 1 to be string, array given in
error.` Please let me know how can I pass json array as parameter and return the same.
Here is my code -
etc/webapi.xml
<?xml version="1.0" ?>
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
<route method="POST" url="/V1/managecustomers">
<service class="NamespaceModulenameApiManageCustomerInterface" method="createUpdate"/>
<resources>
<resource ref="anonymous"/>
</resources>
</route>
</routes>
etc/di.xml -
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="NamespaceModulenameApiManageCustomerInterface" type="NetzweltCustomerModelManagecustomer"/>
</config>
NamespaceModulenameApiManageCustomerInterface.php :-
namespace NamespaceModulenameApi;
interface ManageCustomerInterface
{
/**
*
* @param mixed $data
* @return string
*/
public function createUpdate($data);
}
NamespaceModulenameModelManagecustomer.php - :
namespace NamespaceModulenameModel;
use NamespaceModulenameApiManageCustomerInterface;
class Managecustomer implements ManageCustomerInterface
{
/**
*
* @param mixed $data
* @return string
*/
public function createUpdate($data) {
$data = json_decode($data);
//echo "<pre>"; print_r($data); die;
return "Hello";
}
}
If i am passing 'data' parameter as string like -
$customerData = [
'data' => "Abcd"
];
Then its running fine.
But If i am trying to pass 'data' as array like-
$customerData = [
'data' => [
"email" => "user@example.com",
"firstname" => "John",
"lastname" => "Doe",
"storeId" => 1,
"websiteId" => 1,
"address"=>[
"street" => "phase-1",
"phone_no"=> "321654213"
]
]
];
Then its giving error - Warning: json_decode() expects parameter 1 to be string, array given in
error.`
Here is my testapi.php file code -
$ch = curl_init("http://localhost/mage23/index.php/rest/all/V1/managecustomers");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($customerData));
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
$result = curl_exec($ch);
echo '<pre>';print_r($result);
Please let me know how can I pass array as parameter in API.
magento2 api custom
add a comment |
I have created custom API that excepting 'mixed' type parameter and returning the 'string' as I defined. In interface I define parameter type 'mixed'. As I am trying to pass array as json_encode, it giving me Warning: json_decode() expects parameter 1 to be string, array given in
error.` Please let me know how can I pass json array as parameter and return the same.
Here is my code -
etc/webapi.xml
<?xml version="1.0" ?>
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
<route method="POST" url="/V1/managecustomers">
<service class="NamespaceModulenameApiManageCustomerInterface" method="createUpdate"/>
<resources>
<resource ref="anonymous"/>
</resources>
</route>
</routes>
etc/di.xml -
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="NamespaceModulenameApiManageCustomerInterface" type="NetzweltCustomerModelManagecustomer"/>
</config>
NamespaceModulenameApiManageCustomerInterface.php :-
namespace NamespaceModulenameApi;
interface ManageCustomerInterface
{
/**
*
* @param mixed $data
* @return string
*/
public function createUpdate($data);
}
NamespaceModulenameModelManagecustomer.php - :
namespace NamespaceModulenameModel;
use NamespaceModulenameApiManageCustomerInterface;
class Managecustomer implements ManageCustomerInterface
{
/**
*
* @param mixed $data
* @return string
*/
public function createUpdate($data) {
$data = json_decode($data);
//echo "<pre>"; print_r($data); die;
return "Hello";
}
}
If i am passing 'data' parameter as string like -
$customerData = [
'data' => "Abcd"
];
Then its running fine.
But If i am trying to pass 'data' as array like-
$customerData = [
'data' => [
"email" => "user@example.com",
"firstname" => "John",
"lastname" => "Doe",
"storeId" => 1,
"websiteId" => 1,
"address"=>[
"street" => "phase-1",
"phone_no"=> "321654213"
]
]
];
Then its giving error - Warning: json_decode() expects parameter 1 to be string, array given in
error.`
Here is my testapi.php file code -
$ch = curl_init("http://localhost/mage23/index.php/rest/all/V1/managecustomers");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($customerData));
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
$result = curl_exec($ch);
echo '<pre>';print_r($result);
Please let me know how can I pass array as parameter in API.
magento2 api custom
add a comment |
I have created custom API that excepting 'mixed' type parameter and returning the 'string' as I defined. In interface I define parameter type 'mixed'. As I am trying to pass array as json_encode, it giving me Warning: json_decode() expects parameter 1 to be string, array given in
error.` Please let me know how can I pass json array as parameter and return the same.
Here is my code -
etc/webapi.xml
<?xml version="1.0" ?>
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
<route method="POST" url="/V1/managecustomers">
<service class="NamespaceModulenameApiManageCustomerInterface" method="createUpdate"/>
<resources>
<resource ref="anonymous"/>
</resources>
</route>
</routes>
etc/di.xml -
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="NamespaceModulenameApiManageCustomerInterface" type="NetzweltCustomerModelManagecustomer"/>
</config>
NamespaceModulenameApiManageCustomerInterface.php :-
namespace NamespaceModulenameApi;
interface ManageCustomerInterface
{
/**
*
* @param mixed $data
* @return string
*/
public function createUpdate($data);
}
NamespaceModulenameModelManagecustomer.php - :
namespace NamespaceModulenameModel;
use NamespaceModulenameApiManageCustomerInterface;
class Managecustomer implements ManageCustomerInterface
{
/**
*
* @param mixed $data
* @return string
*/
public function createUpdate($data) {
$data = json_decode($data);
//echo "<pre>"; print_r($data); die;
return "Hello";
}
}
If i am passing 'data' parameter as string like -
$customerData = [
'data' => "Abcd"
];
Then its running fine.
But If i am trying to pass 'data' as array like-
$customerData = [
'data' => [
"email" => "user@example.com",
"firstname" => "John",
"lastname" => "Doe",
"storeId" => 1,
"websiteId" => 1,
"address"=>[
"street" => "phase-1",
"phone_no"=> "321654213"
]
]
];
Then its giving error - Warning: json_decode() expects parameter 1 to be string, array given in
error.`
Here is my testapi.php file code -
$ch = curl_init("http://localhost/mage23/index.php/rest/all/V1/managecustomers");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($customerData));
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
$result = curl_exec($ch);
echo '<pre>';print_r($result);
Please let me know how can I pass array as parameter in API.
magento2 api custom
I have created custom API that excepting 'mixed' type parameter and returning the 'string' as I defined. In interface I define parameter type 'mixed'. As I am trying to pass array as json_encode, it giving me Warning: json_decode() expects parameter 1 to be string, array given in
error.` Please let me know how can I pass json array as parameter and return the same.
Here is my code -
etc/webapi.xml
<?xml version="1.0" ?>
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
<route method="POST" url="/V1/managecustomers">
<service class="NamespaceModulenameApiManageCustomerInterface" method="createUpdate"/>
<resources>
<resource ref="anonymous"/>
</resources>
</route>
</routes>
etc/di.xml -
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="NamespaceModulenameApiManageCustomerInterface" type="NetzweltCustomerModelManagecustomer"/>
</config>
NamespaceModulenameApiManageCustomerInterface.php :-
namespace NamespaceModulenameApi;
interface ManageCustomerInterface
{
/**
*
* @param mixed $data
* @return string
*/
public function createUpdate($data);
}
NamespaceModulenameModelManagecustomer.php - :
namespace NamespaceModulenameModel;
use NamespaceModulenameApiManageCustomerInterface;
class Managecustomer implements ManageCustomerInterface
{
/**
*
* @param mixed $data
* @return string
*/
public function createUpdate($data) {
$data = json_decode($data);
//echo "<pre>"; print_r($data); die;
return "Hello";
}
}
If i am passing 'data' parameter as string like -
$customerData = [
'data' => "Abcd"
];
Then its running fine.
But If i am trying to pass 'data' as array like-
$customerData = [
'data' => [
"email" => "user@example.com",
"firstname" => "John",
"lastname" => "Doe",
"storeId" => 1,
"websiteId" => 1,
"address"=>[
"street" => "phase-1",
"phone_no"=> "321654213"
]
]
];
Then its giving error - Warning: json_decode() expects parameter 1 to be string, array given in
error.`
Here is my testapi.php file code -
$ch = curl_init("http://localhost/mage23/index.php/rest/all/V1/managecustomers");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($customerData));
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
$result = curl_exec($ch);
echo '<pre>';print_r($result);
Please let me know how can I pass array as parameter in API.
magento2 api custom
magento2 api custom
asked 2 mins ago
AtulAtul
436721
436721
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "479"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f257772%2fhow-to-pass-json-ecoded-array-in-custom-magento2-api%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Magento Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f257772%2fhow-to-pass-json-ecoded-array-in-custom-magento2-api%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown