0

How to get data from https request method in laravel. My received data is currently

{"{\"email\":68,\"token\":\"945672295777710090\"}":null}.\\dd(Request $request)

I don't know how to decode them properly. Or did I send the data the wrong way. I tried some common way $request->input, $request->all, $request->email, jsondecode, endcode

Here is the client side code:

    var post_data = JSON.stringify({
        email: email,
        token: token
    });
    var post_options = {
        host: host_main,
        port: '443',
        path: new_user_list,
        method: 'POST',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            'Content-Length': Buffer.byteLength(post_data)
        }
    };

    var post_req = http.request(post_options, function(res) {
        res.setEncoding('utf8');
        res.on('data', function (chunk) {
            console.log(chunk);
        });
    });

    post_req.write(post_data);
    post_req.end();

N69S
  • 16,110
  • 3
  • 22
  • 36
  • can you try sending the data in JSON only? – Sohail Ansari Dec 14 '22 at 10:08
  • I don't know what is laravel and what you're doing wrong, if anything, but you can easily parse that string in node/js with `const str = '{"{\"email\":68,\"token\":\"945672295777710090\"}":null}.\\dd(Request $request)'; const res = JSON.parse(str.split('.')[0])` – João Pimentel Ferreira Dec 14 '22 at 10:08
  • sorry I not really understand @SohailAnsari – Phương Phạm Dec 14 '22 at 10:16
  • {"{\"email\":68,\"token\":\"945672295777710090\"}":null}. is the data received from \\dd(Request $request) in laravel. dd() it's like printing out a value for debugging @JoãoPimentelFerreira – Phương Phạm Dec 14 '22 at 10:16
  • dont JSON.stringify the data, send them as is. `var post_data = {email: email,token: token};` – N69S Dec 14 '22 at 10:18
  • it will send me an error :``` code: 'ERR_INVALID_ARG_TYPE' . The "chunk" argument must be of type string or an instance of Buffer or Uint8Array. Received an instance of Object```, and i don't get any response from serve looks like it crashed in js trying to send json code @N69S – Phương Phạm Dec 14 '22 at 10:24
  • Does this answer your question? [The "chunk" argument must be of type string or an instance of Buffer](https://stackoverflow.com/questions/61865764/the-chunk-argument-must-be-of-type-string-or-an-instance-of-buffer) – N69S Dec 14 '22 at 10:31
  • I also don't use ```Express ```. Their then method is the same as I am using now @N69S – Phương Phạm Dec 14 '22 at 10:43

1 Answers1

0

If you are in control of laravel side as well, than you should format your data before sending, something like json_encode($data) and than your response will be in JSON format.

Or you can do this with response:

 stringData.replace("\", "")
 JSON.parse(stringData)
cucumber
  • 57
  • 5
  • the data ```chuck``` received is actually $request I received at the server, I don't need to send the data back to the client for processing. I need something like jsondecode("{\"email\":68,\"token\":\"945672295777710090\"}":null}") on the server side to process that string – Phương Phạm Dec 14 '22 at 10:31
  • Excuse me if I don't understand very well but I have to ask, your server is in laravel, and client in node? And you need to parse this data in laravel? – cucumber Dec 14 '22 at 10:33
  • Yes, sorry for my poor english. that's really what i need to do – Phương Phạm Dec 14 '22 at 10:45
  • Ok, so since you are sending request data as a string, and in laravel backend you get string data, did you try to parse_str($data) ? – cucumber Dec 14 '22 at 11:08
  • parse_str returns me an empty result – Phương Phạm Dec 14 '22 at 11:21