1

I have created a custom api following "inchoo" tutorial. But I am getting the data in string format instead of json.

From my browser I am getting a tag surrounding the api return as below. enter image description here But Chrome's postman extension showing a double quote ("") and some extra slashes () in the in the response even after header to "Accept":"application/json" and "Content-Type":"application/json". enter image description here I just want to receive the exact same string I am returning in my Model class.

 public function name() {
    //return "Hello, raihanruhin";
    $arr = '[{"name":"MasterCard"},{"name":"VISA"}, {"name":"DBBL-NEXUS"}, {"name":"American Express"}]';
    return $arr;
}
3

3 Answers 3

0

Look how Magento 2 handles api response..

class Magento\Framework\Webapi\ServiceOutputProcessor {
...
public function convertValue($data, $type)
{
    if (is_array($data)) {
        $result = [];
        $arrayElementType = substr($type, 0, -2);
        foreach ($data as $datum) {
            if (is_object($datum)) {
                $datum = $this->processDataObject(
                    $this->dataObjectProcessor->buildOutputDataArray($datum, $arrayElementType)
                );
            }
            $result[] = $datum;
        }
        return $result;
    } elseif ...
}

Magento 2 accept array type to response.

$arr = [
    ['name' => 'MasterCard'],
    ['name' => 'VISA'],
    ['name' => 'DBBL-NEXUS'],
    ['name' => 'American Express'],
];
return $arr;

or just..

$arr = '[{"name":"MasterCard"},{"name":"VISA"}, {"name":"DBBL-NEXUS"}, {"name":"American Express"}]';
return json_decode($arr, true);
0
0

As I noted in this answer: https://magento.stackexchange.com/a/229971/2392, once you have the response string, you can tell PHP to convert it to a JSON object using this snippet:

$object = json_decode( $response, false, 512, JSON_FORCE_OBJECT);

Where $response is the string from your API response.

Remember that variables only ever exist in the memory of the program you are executing. HTTP is a transport protocol, not a program, so it can only be used to send scalar (simple) variable types (e.g. String, integer, boolean) not complex types (like objects).

So before an object can be sent over HTTP, a string representation of it must be created. This process is called serialisation (or serialization in US English).

-1

Modify your return line to:

return json_decode(json_encode($arr), true);
2
  • That will return a PHP associative array, not a string, which is what the OP appears to want Commented Jun 18, 2018 at 11:47
  • The previous answers ahead of mine also have return values of PHP associative arrays. Additionally, the Magento core code given above in the answer before mine shows an array return value. Perhaps a return value of an array is more desirable than a string. As I recall my struggles with Magento from a year ago, I had a similar problem to the OP and the solution I gave solved my problem. So even though I've not given the response the OP requested, it may be the answer he needs.
    – noctufaber
    Commented Jun 19, 2018 at 13:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.