[英]Laravel get request body with correct encoding
功能很简单,我需要将发布请求转发到外部API服务。 我正在使用Laravel 5.5,$ client是Guzzle 6客户端;
use Illuminate\Http\Request;
use GuzzleHttp\Client;
public function insert(Request $request)
{
try {
$body = $request->json()->all();
$response = $this->client->post($this->generateUrl("/api/sections?", $request->getQueryString()), ['json' =>$body]);
return response($response->getBody(),$response->getStatusCode())>withHeaders($response->getHeaders());
}catch(Exception $e){
.....
}
}
问题是$ bodyStringRAW = $ request-> json()-> all();
发送到laravel的数据是[{“ name”:“ Secion4小明”,“ comment”:“”,“ applicationId”:“ 59”}]
外部API接收的数据为[{“ name”:“ Secion4 \\ u5c0f \\ u660e”,“ comment”:null,“ applicationId”:“ 59”}]
如果我将代码更改为
$response = $this->client->post($this->generateUrl("/api/sections?", $request->getQueryString()), ['json' =>$request->getContent()]);
外部API接收的数据为[{\\“ name \\”:\\“ Secion4 \\ u5c0f \\ u660e \\”,\\“ comment \\”:\\“ \\”,\\“ applicationId \\”:\\“ 59 \\”}]
如何使编码正确? 我找不到设置编码的方法。
更新1:
$body = utf8_decode($request->getContent());
$response = $this->client->post($this->generateUrl("/api/sections?", $request->getQueryString()), ['json' =>$body]);
外部API得到了[{\\“ name \\”:\\“ Secion4 ?? \\”,\\“ comment \\”:\\“ \\”,\\“ applicationId \\”:\\“ 59 \\”}]
数据是Unicode,您可以使用utf8_decode进行转换。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.