Skip to content

Commit 4b17056

Browse files
author
Justinas Pošiūnas
committed
send request body as json
1 parent 878da57 commit 4b17056

File tree

2 files changed

+9
-13
lines changed

2 files changed

+9
-13
lines changed

src/Common/RestClient.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function get($endpointUri, $queryString = [])
5353
*/
5454
public function post($endpointUri, $data = [])
5555
{
56-
return $this->send('POST', $endpointUri, http_build_query($data));
56+
return $this->send('POST', $endpointUri, $data);
5757
}
5858

5959
/**
@@ -65,7 +65,7 @@ public function post($endpointUri, $data = [])
6565
*/
6666
public function put($endpointUri, $putData = [])
6767
{
68-
return $this->send('PUT', $endpointUri, http_build_query($putData));
68+
return $this->send('PUT', $endpointUri, $putData);
6969
}
7070

7171
/**
@@ -93,7 +93,7 @@ protected function send($method, $endpointUri, $body = null, array $headers = []
9393
$headers = array_merge($headers, self::getDefaultHeaders());
9494
$endpointUrl = $this->baseUrl . $endpointUri;
9595

96-
$request = new Request($method, $endpointUrl, $headers, $body);
96+
$request = new Request($method, $endpointUrl, $headers, json_encode($body));
9797
$response = $this->getHttpClient()->sendRequest($request);
9898

9999
return $this->handleResponse($response);
@@ -139,7 +139,8 @@ protected function getHttpClient()
139139
protected function getDefaultHeaders() {
140140
return [
141141
'User-Agent' => ApiConstants::SDK_USER_AGENT . '/' . ApiConstants::SDK_VERSION,
142-
'X-MailerLite-ApiKey' => $this->apiKey
142+
'X-MailerLite-ApiKey' => $this->apiKey,
143+
'Content-Type' => 'application/json'
143144
];
144145
}
145146
}

tests/RestClientTest.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ public function post_method()
4949

5050
$response = $this->client->post('post', $formData);
5151

52-
$this->assertEquals(http_build_query($formData), $response['body']->data);
52+
$this->assertEquals(json_encode($formData), $response['body']->data);
5353
}
5454

5555
/** @test **/
56-
public function put_mehtod()
56+
public function put_method()
5757
{
5858
$formData = [
5959
'foo' => 'bar',
@@ -62,20 +62,15 @@ public function put_mehtod()
6262

6363
$response = $this->client->put('put', $formData);
6464

65-
$this->assertEquals(http_build_query($formData), $response['body']->data);
65+
$this->assertEquals(json_encode($formData), $response['body']->data);
6666
}
6767

6868
/** @test **/
6969
public function delete_method()
7070
{
71-
$formData = [
72-
'foo' => 'bar',
73-
'fiz' => 'biz'
74-
];
75-
7671
$response = $this->client->delete('delete');
7772

78-
$this->assertEmpty($response['body']->data);
73+
$this->assertEquals('null', $response['body']->data);
7974
}
8075

8176
/** @test **/

0 commit comments

Comments
 (0)