Search code examples
phplaravelapidelphiindy10

Problem getting response content using Delphi, Indy10 and LARAVEL PHP


I am building a LARAVEL PHP API in order to be consumed by Delphi 2007. Basically, in Delphi I am performing a POST and in PHP I am validating the fields. If it fails validation, I need to return code 422 along with the validation errors (array).

In Delphi, I'm using Indy10. In it I have a Client of type TIdHTTP.

To do the POST, I do:

Client.Post(sFullEndPoint, Request, Response);

To get code 422:

Client.ResponseCode;

To get the content of the response:

Response.DataString;

In PHP, if I return only one array of errors, as return $ errors I can handle it in Delphi with Response.DataString, the problem is that I won't know the response code, because it will come 200. If I return response ($ errors, 422) in PHP, Delphi does not find the value of $errors in response.

I need to get the HTTP code and the response body. Can someone help me?


Solution

  • you can set http response code in php like (refrence : https://www.php.net/manual/en/function.http-response-code.php) :

    http_response_code(422);
    

    also in laravel you can do it like :

    return response('Your output string', 200)->header('Content-Type', 'text/plain');
    

    but i suggest you to pass response as json and add some information about what is wrong or ... :

    $errors = [
        [
            'error_code'=>1312,
            'error_message'=>'name is empty'
        ]
    ];
    return Response::json($errors, 201); // Status code here