The function is simple, I need to forward a post request to an external API service. I am using Laravel 5.5 and the $client is Guzzle 6 client;
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){
.....
}
}
the problem is the $bodyStringRAW = $request->json()->all();
the data send to laravel is [{"name":"Secion4小明","comment":"","applicationId":"59"}]
the data received by the external API is [{"name":"Secion4\u5c0f\u660e","comment":null,"applicationId":"59"}]
If i change the code to
$response = $this->client->post($this->generateUrl("/api/sections?", $request->getQueryString()), ['json' =>$request->getContent()]);
the external API received the data as [{\"name\":\"Secion4\u5c0f\u660e\",\"comment\":\"\",\"applicationId\":\"59\"}]
How can I make the encoding correct? I Can't find the way to set the encoding.
Update 1:
$body = utf8_decode($request->getContent());
$response = $this->client->post($this->generateUrl("/api/sections?", $request->getQueryString()), ['json' =>$body]);
External API got [{\"name\":\"Secion4??\",\"comment\":\"\",\"applicationId\":\"59\"}]