使用 Delphi、Indy10 和 LARAVEL PHP 获取响应内容时出现问题
Problem getting response content using Delphi, Indy10 and LARAVEL PHP
我正在建造一个 LARAVEL PHP API 以便在 Delphi 2007 年使用。
基本上,在 Delphi 中,我正在执行 POST,在 PHP 中,我正在验证字段。如果验证失败,我需要 return 代码 422 以及验证错误(数组)。
在 Delphi 中,我使用的是 Indy10。在其中我有一个 TIdHTTP
.
类型的客户端
要完成 POST,我会:
Client.Post(sFullEndPoint, Request, Response);
获取代码 422:
Client.ResponseCode;
获取响应内容:
Response.DataString;
在PHP中,如果我return只有一组错误,如return $ errors
我可以在Delphi和Response.DataString
中处理,问题是我不知道响应代码,因为它会变成 200。
如果我returnresponse ($ errors, 422)
在PHP,Delphi没有找到$errors
的值在response.
我需要获取 HTTP 代码和响应正文。有人可以帮助我吗?
您可以在 php 中设置 http 响应代码,例如(参考:https://www.php.net/manual/en/function.http-response-code.php):
http_response_code(422);
同样在 laravel 中,您可以这样做:
return response('Your output string', 200)->header('Content-Type', 'text/plain');
但我建议您将响应作为 json 传递并添加一些有关错误或...的信息:
$errors = [
[
'error_code'=>1312,
'error_message'=>'name is empty'
]
];
return Response::json($errors, 201); // Status code here
我正在建造一个 LARAVEL PHP API 以便在 Delphi 2007 年使用。 基本上,在 Delphi 中,我正在执行 POST,在 PHP 中,我正在验证字段。如果验证失败,我需要 return 代码 422 以及验证错误(数组)。
在 Delphi 中,我使用的是 Indy10。在其中我有一个 TIdHTTP
.
要完成 POST,我会:
Client.Post(sFullEndPoint, Request, Response);
获取代码 422:
Client.ResponseCode;
获取响应内容:
Response.DataString;
在PHP中,如果我return只有一组错误,如return $ errors
我可以在Delphi和Response.DataString
中处理,问题是我不知道响应代码,因为它会变成 200。
如果我returnresponse ($ errors, 422)
在PHP,Delphi没有找到$errors
的值在response.
我需要获取 HTTP 代码和响应正文。有人可以帮助我吗?
您可以在 php 中设置 http 响应代码,例如(参考:https://www.php.net/manual/en/function.http-response-code.php):
http_response_code(422);
同样在 laravel 中,您可以这样做:
return response('Your output string', 200)->header('Content-Type', 'text/plain');
但我建议您将响应作为 json 传递并添加一些有关错误或...的信息:
$errors = [
[
'error_code'=>1312,
'error_message'=>'name is empty'
]
];
return Response::json($errors, 201); // Status code here