Send a JSON array with single quotes

Asked

Viewed 487 times

-2

I am doing integration of an api in Laravel, I have to submit a POST according to the documentation of the api with the following body

{
      "token": "TOKEN DA APLICACAO",
      "email": "[email protected]",
      "frete": "0",
      "carrinho": '[{
                  "produto":"Produto 1",
                  "quantidade":"1",
                  "preco":"10.92",
                  "imagem":"http://site.com.br/foto.jpg",
                  "url":"http://site.com.br/produto"
                  }]'
  }

however, the body I’m sending is like this

{"token":"xxxxxxx","email":"[email protected]","frete":"0","carrinho":[{"produto":"computador","quantidade":"1","preco":"3000.00","imagem":"http:site.com.br/foto.jpg","url":"http//loja.dooca.local/computador"}]}

i am populating that array as follows:

 $checkouts = [
                'token' => 'xxxxxx'),
                'email' => $checkout->getCustomer()->getEmail(),
                'frete' => '0',
                'carrinho' => [],
            ];

            foreach ($checkout->getItems() as $item) {
                $checkouts['carrinho'][] = [
                    'produto' => $item->getName(),
                    'quantidade' => strval($item->getQuantity()),
                    'preco' => $item->getPrice(),
                    'imagem' => 'http://site.com.br/foto.jpg',
                    'url' => $item->getUrl(),

               ];

The problem is that this api only accepts if in the cart part there are simple quotes before the brackets and I can’t put it at all... as for example "cart": '[{

  • 1

    It makes no sense, as they already said "", something wrong is not right, if carrinho: need to receive a "STRING" with a JSON format or it is because the author of the API did something very serious. Since you didn’t present the sending part or how you used json_encode then there is no way anyone can answer you reasonably, and I can’t guess a response, after all the site is not for this. Give more details.

  • Exactly, in the documentation it is only accepted to send the body in this mold: { "token": "APPLICATION TOKEN", "email": "[email protected]", "freight": "0", "cart": '[{ "product":"Product 1", "quantity":"1", "price":"10.92", "image":"http://site.com.br/foto.jpg", "url":"http://site.com.br/product" }]' } Sending this: Api::sent()->checkout()->post($checkouts); I’m sending that array above.. that would be right..

  • I don’t have access to such documentation to know if this is it or if you got something wrong, and don’t add code in the "comments" field, look at the question, have a link called "edit", correct the question with the minimum details that are missing so we can understand the problem, It won’t do any good if I’m kicking answers like the ones I’ve already answered, in order to get an answer you have to "help us" to help you, it shows what you’ve done understandably, loose pieces of code that don’t do anything won’t help.

1 answer

-1


Hello, all right?

See if it helps you.

$array = [];

for($i = 0; $i < 10; $i++){
    $array[] = [
        'produto' => 'nome item',
        'quantidade' => 12,
        'preco' => 12.12,
        'imagem' => 'http://site.com.br/foto.jpg',
        'url' => 'http://site.com.br/',
    ];
}

$carrinho = json_encode($array);

$checkouts = [
    'token' => 'xxx',
    'email' => '[email protected]',
    'frete' => '0',
    'carrinho' => "$carrinho"
];

Exit

array(4) {
  ["token"]=>
  string(3) "xxx"
  ["email"]=>
  string(21) "[email protected]"
  ["frete"]=>
  string(1) "0"
  ["carrinho"]=>
  string(1271) "[{"produto":"nome item","quantidade":12,"preco":12.12,"imagem":"http:\/\/site.com.br\/foto.jpg","url":"http:\/\/site.com.br\/"},{"produto":"nome item","quantidade":12,"preco":12.12,"imagem":"http:\/\/site.com.br\/foto.jpg","url":"http:\/\/site.com.br\/"},{"produto":"nome item","quantidade":12,"preco":12.12,"imagem":"http:\/\/site.com.br\/foto.jpg","url":"http:\/\/site.com.br\/"},{"produto":"nome item","quantidade":12,"preco":12.12,"imagem":"http:\/\/site.com.br\/foto.jpg","url":"http:\/\/site.com.br\/"},{"produto":"nome item","quantidade":12,"preco":12.12,"imagem":"http:\/\/site.com.br\/foto.jpg","url":"http:\/\/site.com.br\/"},{"produto":"nome item","quantidade":12,"preco":12.12,"imagem":"http:\/\/site.com.br\/foto.jpg","url":"http:\/\/site.com.br\/"},{"produto":"nome item","quantidade":12,"preco":12.12,"imagem":"http:\/\/site.com.br\/foto.jpg","url":"http:\/\/site.com.br\/"},{"produto":"nome item","quantidade":12,"preco":12.12,"imagem":"http:\/\/site.com.br\/foto.jpg","url":"http:\/\/site.com.br\/"},{"produto":"nome item","quantidade":12,"preco":12.12,"imagem":"http:\/\/site.com.br\/foto.jpg","url":"http:\/\/site.com.br\/"},{"produto":"nome item","quantidade":12,"preco":12.12,"imagem":"http:\/\/site.com.br\/foto.jpg","url":"http:\/\/site.com.br\/"}]"
}
  • Good night, helped in parts... I managed to leave the ["cart"] as I would like... but it looks exactly like you posted, but the API I’m integrating, the guy did totally wrong and before the bracket in this part here "[{"produto" : he won’t accept those double quotes... and that’s what I’m struggling with

  • Put it like this, 'cart' => "'$cart'"

  • The API people figured out how to get the information, and only with their first answer solved my case, thank you

  • Glad I could help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.