1

I'm working with angular and laravel,I have this json in my request, but for some reason I cannot access to the data inside the key called "llave_criptografica" , this is the complete json

{username: "eeded", contrasena: null, llave_criptografica: {…}, clave_criptografica: null}

the data inside "llave_criptografica" is this

{filename: "011503046233.p12", filetype: "application/x-pkcs12", value: "MIIaPAIBAzCCGfYG-base-64-encode"}

as you can see, I just have encoded and send a key with base64, but when I try to access to it, I get this error

        return $request->llave_criptografica->filename;

Trying to get property of non-object

if I try to access as an array this is the error

        return $request->llave_criptografica["filename"];

Unexpected number in JSON at position 1 at JSON.parse

this is the function where I store the file data

onFileChange(event) {
let reader = new FileReader();
if(event.target.files && event.target.files.length > 0) {
  let file = event.target.files[0];
  reader.readAsDataURL(file);
  reader.onload = () => {
    this.formCertificado.get('llave_criptografica').setValue({
      filename: file.name,
      filetype: file.type,
      value: reader.result.split(',')[1]
    })

        }
  }
}

enter image description here

When I do a log, this is the content of the key

array ( 'username' => 'eded', 'contrasena' => NULL, 'llave_criptografica' => array ( 'filename' => '011503046233.p12', 'filetype' => 'application/x-pkcs12', 'value' => 'MIIaP-base-64', ), 'clave_criptografica' => NULL, )
Felipe Castillo
  • 536
  • 8
  • 25
  • What do you get for `var_dump($request-> llave_criptografica);`? If it's another json, you'll need to decode it. – aynber Apr 04 '19 at 15:08
  • The example you have copied here isn't valid JSON, e.g. keys are not inside of quotes. Would be helpful if you could post the exact request body. – jelhan Apr 04 '19 at 15:09
  • hi, when I try to decoded it I get this error, **json_decode() expects parameter 1 to be string, array given** – Felipe Castillo Apr 04 '19 at 15:13
  • @jelhan I've edited my question – Felipe Castillo Apr 04 '19 at 15:16
  • If you're using Laravel, then JSON is a part of body (input), therefore it's `$request->input('llave_criptografica.filename');` – Mjh Apr 04 '19 at 15:21
  • I'm using angular and laravel, the request comes from angular – Felipe Castillo Apr 04 '19 at 15:24
  • @Mjh I've tried using input, but I still getting the same error **Unexpected number in JSON at position 1 at JSON.parse**, if I just return `$request->input('llave_criptografica')` it works – Felipe Castillo Apr 04 '19 at 15:27
  • 1
    Please do a `dd($request->all())` and edit the question to include that dump data. – Bogdan Apr 04 '19 at 15:28
  • @Mjh `$request->whatever` and `$request->input('whatever')` are syntax variations, but both return the same thing, which is the contents of `whatever` in `$request`. – Tim Lewis Apr 04 '19 at 15:28
  • it's hard, because I'm sending a post request, when I use `dd()` I get a request error – Felipe Castillo Apr 04 '19 at 15:32
  • Have you tried return $request['llave_criptografica']["filename"]; ? @FelipeCastillo – party-ring Apr 04 '19 at 15:35
  • Try `Log::info($request->all();`, and check the result in `/storage/logs/` – aynber Apr 04 '19 at 15:35
  • @FelipeCastillo: Please add the exact request body not a preview in developer tools or the object that is serialized to JSON. Since you are getting errors that your body is not a valid JSON I would bet, that it's not a valid JSON. – jelhan Apr 04 '19 at 15:35
  • @FelipeCastillo But you should still see the response with the dump in your Network tab of your browser's developer tools. – Bogdan Apr 04 '19 at 15:36
  • this is the content of the log `array ( 'username' => 'eded', 'contrasena' => NULL, 'llave_criptografica' => array ( 'filename' => '011503046233.p12', 'filetype' => 'application/x-pkcs12', 'value' => 'MIIaP-base-64', ), 'clave_criptografica' => NULL, ) ` – Felipe Castillo Apr 04 '19 at 15:53
  • I've edited my question – Felipe Castillo Apr 04 '19 at 15:57
  • 1
    So `llave_criptografica` is an array, so you need to access it with array methods in PHP, not object. Note that your error messages are two different errors: `Trying to get property of non-object` is from PHP, and is fixed by the accessing it correctly. `Unexpected number in JSON at position 1 at JSON.parse` is a javascript error, and may be because you're returning a string, not a json. – aynber Apr 04 '19 at 15:58
  • Yeah, if you're returning the filename, you probably want to use `return response()->json(["filename" => $request->llave_criptografica["filename"]]);` to ensure your endpoint can handle it. – Tim Lewis Apr 04 '19 at 16:05
  • @TimLewis they're not the same thing. There's a difference between `$request->name->property` and `$request->name['property']`. I'm sure that you can figure the difference out. Having a function that distinguishes between the two is useful, because you don't have to worry whether json was decoded into an array or stdclass. Being consistent and writing non-ambiguous code that doesn't use magic functions is a big plus while debugging. Being consistent helps to eliminate common problems faster. – Mjh Apr 04 '19 at 19:33
  • @Mjh Yes, but I didn't say that `$request->name->property` and `$request->name['property']` were the same... One is obviously array access while the other is object access. I said `$request->whatever` and `$request->input("whatever")` are the same. The `input()` method and directly accessing the input key (in my example `whatever`) are syntax preferences. – Tim Lewis Apr 04 '19 at 19:36
  • @TimLewis that's why there's a dot in the example I posted. However, the issue seems to be resolved. I understand what you're saying, ultimately both approaches deal with the same underlying data so neither is actually wrong. – Mjh Apr 04 '19 at 19:41
  • @Mjh I understand. I read your initial comment as "you have to use `$request->input("llave_criptografica")` instead if `$request->llave_criptografica`, and was simply stating that either are correct. But yes, issue seems to be resolved. Cheers. – Tim Lewis Apr 04 '19 at 19:43

1 Answers1

2

To access the data you must access it as an array, using [] opposed to an object where you would use ->. You can do it like so:

$request["llave_criptografica"]["filename"]; 

You can always cast this to an object if you need to access it like an object.

party-ring
  • 1,761
  • 1
  • 16
  • 38
  • Almost; `$request->llave_criptografica` (or `$request['llave_criptografica']`) is ok, but `$request->llave_criptografica->filename` is not, while `$request->llave_criptografica['filename']` is (or should be). – Tim Lewis Apr 04 '19 at 16:02