@@ -260,7 +260,7 @@ private function addFields(array $data): void
260260 return ;
261261 }
262262
263- if ($ this ->accept_json ) {
263+ if ($ this ->accept_json || $ this -> hasHeader ( ' content-type ' , ' application/json ' ) ) {
264264 $ payload = json_encode ($ data );
265265 } else {
266266 $ payload = http_build_query ($ data );
@@ -377,12 +377,28 @@ public function setUserAgent(string $user_agent): HttpClient
377377 /**
378378 * Configure client to accept and send JSON data
379379 *
380+ * @deprecated 5.2.99
380381 * @return HttpClient
381382 */
382383 public function acceptJson (): HttpClient
383384 {
384385 $ this ->accept_json = true ;
385386
387+ $ this ->withHeaders (["Content-Type " => "application/json " ]);
388+ $ this ->withHeaders (["Accept " => "application/json " ]);
389+
390+ return $ this ;
391+ }
392+
393+ /**
394+ * Configure client to accept and send JSON data
395+ *
396+ * @return HttpClient
397+ */
398+ public function withJson (): HttpClient
399+ {
400+ $ this ->accept_json = true ;
401+
386402 $ this ->withHeaders (["Content-Type " => "application/json " ]);
387403
388404 return $ this ;
@@ -398,13 +414,25 @@ public function withHeaders(array $headers): HttpClient
398414 {
399415 foreach ($ headers as $ key => $ value ) {
400416 if (!in_array (strtolower ($ key . ': ' . $ value ), array_map ('strtolower ' , $ this ->headers ))) {
401- $ this ->headers [] = $ key . ': ' . $ value ;
417+ $ this ->headers [] = trim ( $ key) . ': ' . $ value ;
402418 }
403419 }
404420
405421 return $ this ;
406422 }
407423
424+ /**
425+ * Check if header exists
426+ *
427+ * @param string $key
428+ * @param string $value
429+ * @return boolean
430+ */
431+ public function hasHeader (string $ key , string $ value ): bool
432+ {
433+ return in_array (strtolower ($ key . ': ' . $ value ), array_map ('strtolower ' , $ this ->headers ));
434+ }
435+
408436 /**
409437 * Set HTTP authentication credentials
410438 *
0 commit comments