Receive Json sent from Angular to Laravel

Viewed 106

I'm trying to send a json to the backend, here is the piece of code that I use to send data:

register(user): Observable<any>{
        let json = JSON.stringify(user);
        let params = 'json='+json;

        let headers = new HttpHeaders().set('Content-Type', 'aplication/x-www-form-urlencoded');
        console.log(json);
        console.log(params);
        console.log(headers);

        return this._http.post('http://api-rest-laravel.com.devel/api/register', params, {headers: headers});
    }

This is the code in the backend:

     public function register(Request $request) {
    
            Log::info("Datos: " . $request);
            $json = $request->input('json', null);
            $params = json_decode($json);
            $params_array = json_decode($json, true); //Array
            Log::info("Json: " . $json);
            if (!empty($params) && !empty($params_array)) {
                //register
            }else{
                //send error msg
            }

If i try the backend using Postman, this is what i get from Request param:

[2020-10-04 15:37:36] local.INFO: Datos: POST /api/register HTTP/1.1
Accept:          */*
Accept-Encoding: gzip, deflate, br
Connection:      keep-alive
Content-Length:  229
Content-Type:    application/x-www-form-urlencoded
Cookie:          laravel_session=eyJpdiI6InoxMG53RXZTTGZaV0h3dFF6VmVDUXc9PSIsInZhbHVlIjoiZ2V6ZEFYNjZKOSt0K0RPMmgra0FCMVUwMkhIb0JGSjJ6cW53VkhKSnUwV1ViWENVZTNDaXl3UzlBckZVYXRCSSIsIm1hYyI6ImM2YzA0MjEzYzRjMTEzNmQxYmY4NTJmNDRhNzk1ZGJmZGQwOGU2NmQ2NDM5OWM2MDcwNjFjNmJiMTIzNDIyM2MifQ%3D%3D
Host:            api-rest-laravel.com.devel
Postman-Token:   47cfbc5a-136c-424d-9e1b-f61c18c2fc5c
User-Agent:      PostmanRuntime/7.26.5
Cookie: laravel_session=pi7VHwGsyzTB36rK7KdbtAUU4GGagUNSklMQliqL


json=%7B%22id%22%3A1%2C%22name%22%3A%22qwer%22%2C%22surname%22%3A%22qwe%22%2C%22role%22%3A%22ROLE_USER%22%2C%22email%22%3A%22qqwer22%40asd.com%22%2C%22password%22%3A%22qqq%22%2C%22description%22%3A%22%22%2C%22image%22%3A%22%22%7D  
[2020-10-04 15:37:36] local.INFO: Json: {"id":1,"name":"qwer","surname":"qwe","role":"ROLE_USER","email":"qqwer22@asd.com","password":"qqq","description":"","image":""}  
[2020-10-04 15:37:36] local.INFO: goes toIF 

and this is request param if I try it in browser ($json is empty, second last line):

[2020-10-04 15:40:30] local.INFO: Datos: POST /api/register HTTP/1.1
Accept:          application/json, text/plain, */*
Accept-Encoding: gzip, deflate
Accept-Language: es-ES,es;q=0.9,en;q=0.8
Connection:      keep-alive
Content-Length:  127
Content-Type:    aplication/x-www-form-urlencoded
Dnt:             1
Host:            api-rest-laravel.com.devel
Origin:          http://localhost:4200
Referer:         http://localhost:4200/
User-Agent:      Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36

json={"id":1,"name":"qwe","surname":"qwe","role":"ROLE_USER","email":"qqwe@asd.com","password":"q","description":"","image":""}  
[2020-10-04 15:40:30] local.INFO: Json:   
[2020-10-04 15:40:30] local.INFO: goes to ELSE

I also tried to send from frontend a json simillar to the one sent from postman to see if that was the problem. Didn't work.

//return this._http.post('http://api-rest-laravel.com.devel/api/register', 'json=%7B%22id%22%3A1%2C%22name%22%3A%22qwer%22%2C%22surname%22%3A%22qwe%22%2C%22role%22%3A%22ROLE_USER%22%2C%22email%22%3A%22qqwer222%40asd.com%22%2C%22password%22%3A%22qqq%22%2C%22description%22%3A%22%22%2C%22image%22%3A%22%22%7D', {headers: headers});

Why is this happening? I'm newbie with angular and laravel and I didnt find a solution so far. Corse is OK

1 Answers

in your header :

  • let headers = new HttpHeaders().set('Content-Type','application/json')
Related