1

I have tried literally everything shown as solution. I have tried every option in Angular2 http POST body sent as null and many other related posts, nothing has worked so far for me. I have the main react app deployed with npm start and part of the login is as follows:

export async function loginService(credentials) {
 return fetch('http://localhost:3333/mock-server/index.php', { 
        method: 'POST',
        body: JSON.stringify(credentials)  //user:"user"
        })
        .then(
            function(data){
                if(data.status === 422){
                    data.json().then(val=>console.log("DEVOLUCION 422: "+JSON.stringify(val)));
                    return;
                }
                return data.json();
            }
        ).catch(error=>{
            console.error('error! ->', error);
        })
}

PHP File:

<?php
$headers = apache_request_headers();
header("Access-Control-Allow-Origin: http://localhost:3000");
header("Access-Control-Allow-Methods: HEAD, GET, POST, PUT, PATCH, DELETE, OPTIONS");
header("Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method,Access-Control-Request-Headers, Authorization");
header('Content-Type: application/json');
$method = $_SERVER['REQUEST_METHOD'];
if ($method == "OPTIONS") {
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method,Access-Control-Request-Headers, Authorization");
header("HTTP/1.1 200 OK");
die;
}
$username = $_POST['user'];

This $username returns null. I don't know what else to do, I have tried sending a plain 'user=user' as part of the url as well, but always, it returns null. Worth mentioning my php server is the Vscode extension "php server". I tried directly as well directly from php directory executable, with the same luck. When I try with Postman, it returns the name correctly.

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
Germán
  • 1,007
  • 1
  • 7
  • 20
  • Does this answer your question? [Reading JSON POST using PHP](https://stackoverflow.com/questions/19004783/reading-json-post-using-php) – cOle2 Apr 01 '22 at 14:42
  • The required request headers are missing, you need to add those first so that the web server can understand which type of data you're sending. – Ar Rakin Apr 01 '22 at 14:42
  • Also, PHP cannot understand directly JSON data. You can either use `Content-Type: application/x-www-form-urlencoded` data or you need to manually parse the raw JSON and then work with it. – Ar Rakin Apr 01 '22 at 14:55
  • @ArRakin I tried as well Content-Type: application/x-www-form-urlencoded, unless i formatted wrong the data to be sent. Should it look like "?user=user" ? – Germán Apr 01 '22 at 14:58
  • @Germán No. it should look like `user=user` (no question mark). For more info about building HTTP query strings in JS, see: https://stackoverflow.com/questions/111529/how-to-create-query-parameters-in-javascript – Ar Rakin Apr 01 '22 at 15:05
  • @ArRakin I correctly set the Content-type to the one above, however, user is returned as null. Still works from postman, but from my code the user name is not reaching PHP. – Germán Apr 04 '22 at 12:10

0 Answers0