0

I have an angular app where I send data (via json) to my laravel server. My server is on a VM(ubuntu):

This is where I send it to the server from the angular app.

this.http.post(this.loginURL, requestBody, options)

On my laravel server I have the route:

Route::get('patientlogin','UploadController@login');

And the controller method

 public function login(Request $request){
    // error_reporting(-1); // prints every error, warning, etc
    error_reporting(0); // no output at all

    // set content-type of response to json
    header('Content-Type: application/json');

    // import Auth class and custom functions
    // require_once('custom_functions.php');


    $LOGIN_LOG_FILE = "login1.log";
    $AUTH_HEADERS_FILE = "auth-headers1.txt";


    /*
        php://input is raw input, regardless of header field "content-type"
        The PHP superglobal $_POST, only is supposed to wrap data that is either
        application/x-www-form-urlencoded or multipart/form-data-encoded
        http://stackoverflow.com/a/8893792

        When sending only a JSON, $_POST etc will not be populated and php://input has to be used
        in the php scripts
        http://stackoverflow.com/questions/1282909/php-post-array-empty-upon-form-submission
        http://php.net/manual/de/wrappers.php.php
    */



    $content =  $request->instance();
    $json_raw = $content->json()->all();
    $json = json_decode($json_raw, true);


    /* <-- DEBUGGING START TODO delete */
    //read the header, where username and password are supposed to be in
    $headers = apache_request_headers();

    //print the contents of the headers array in a neat structure and log them
    $headersPrintable = print_r($headers, true);
    file_put_contents($AUTH_HEADERS_FILE, $headersPrintable, FILE_APPEND);

    $request = print_r($_REQUEST, true);
    $post = print_r($_POST, true);
    file_put_contents("auth-req.txt", $request, FILE_APPEND);
    file_put_contents("auth-post.txt", $post, FILE_APPEND);

    file_put_contents("auth-req-json.txt", $json_raw, FILE_APPEND);
    file_put_contents("auth-req-json_decoded.txt", $json, FILE_APPEND);
    /* DEBUGGING END --> */

    $valid = false;
    $username = "";

    //check if username and passord exist in the json-decoded version of php://input
    if(array_key_exists("username", $json) && array_key_exists("password", $json)) {
        $username = $json["username"];
        $password = $json["password"];
        $valid = Auth::checkCredentials($username, $password);
    }

    $response = array(
        "username" => $username,
        "valid" => $valid
    );

    echo json_encode($response);

    //exit();

}

Now when I run the app I've get the error:

POST http://ip/patientlogin 405 (Method Not Allowed)

When I change the get in my web.php to post I get this error:

polyfills.js:1 POST http://ip/patientlogin 500 (Internal Server Error)

and when I try to call the url in the browser:

MethodNotAllowedHttpException in RouteCollection.php line 218:

Someone any idea what the error could be or what am I doing wrong?

Willem D'Haeseleer
  • 19,661
  • 9
  • 66
  • 99
Eronimo44
  • 11
  • 1
  • 6
  • That's because your route is a get. `Route::get` and you're trying to post. – Stubbies Dec 31 '16 at 18:46
  • I think you need to check your error logs or look at the response result in your developer tools for more informations about the error. If you call the post route in the browser you get an error becaus the browser is requesting via get.. so post should be fine or define both. – Davide Perozzi Dec 31 '16 at 18:50
  • change this.http.post to this.http.get or something if available or change your server route type as post router exlude it from csrf verify middleware – Khan Shahrukh Dec 31 '16 at 19:05
  • how can i exlude it ? never done it before, – Eronimo44 Dec 31 '16 at 19:07

1 Answers1

1

HTTP GET cannot have a body(it can, technically, but it's not meant to be). So if you're sending a body in the request you should to use post or put.

You are getting method not allowed because you configured your route to use GET not POST.

Change

Route::get

To

Route::post 

That should solve it

Amanuel Nega
  • 1,899
  • 1
  • 24
  • 42