简体   繁体   English

使用 Delphi、Indy10 和 LARAVEL PHP 获取响应内容时出现问题

[英]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. 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).如果验证失败,我需要返回代码 422 以及验证错误(数组)。

In Delphi, I'm using Indy10.在 Delphi 中,我使用的是 Indy10。 In it I have a Client of type TIdHTTP .在其中我有一个TIdHTTP类型的客户端。

To do the POST, I do:要进行 POST,我会:

Client.Post(sFullEndPoint, Request, Response);

To get code 422:要获取代码 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.在 PHP 中,如果我只返回一个错误数组,作为return $ errors ,我可以在 Delphi 中使用Response.DataString处理它,问题是我不知道响应代码,因为它会出现 200。如果我返回response ($ errors, 422)在 PHP, Delphi 中没有找到$errors的值作为响应。

I need to get the HTTP code and the response body.我需要获取 HTTP 代码和响应正文。 Can someone help me?有人能帮我吗?

you can set http response code in php like (refrence: https://www.php.net/manual/en/function.http-response-code.php ): 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:同样在 laravel 你可以这样做:

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... :但我建议您将响应传递为 json 并添加一些有关错误或...的信息:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM