Skip to content

Commit ff7a661

Browse files
committed
Parse headers + handle raw JSON data
1 parent 6c37aeb commit ff7a661

File tree

9 files changed

+60
-13
lines changed

9 files changed

+60
-13
lines changed

app/Models/Request.php

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace App\Models;
44

5+
use Illuminate\Support\Str;
6+
57
class Request
68
{
79
private string $url;
@@ -12,6 +14,8 @@ class Request
1214

1315
private array $data = [];
1416

17+
private bool $jsonData = false;
18+
1519
private bool $multipartFormData = false;
1620

1721
private function __construct($url, $method)
@@ -25,12 +29,27 @@ public static function create(array $data): self
2529
$request = new self($data['url'], $data['method']);
2630

2731
if (!empty($data['headers'])) {
28-
$request->headers = $data['headers'];
32+
$request->headers = collect($data['headers'])
33+
->mapWithKeys(function ($header) {
34+
[$key, $value] = explode(':', $header, 2);
35+
36+
return [trim($key) => self::convertDataType(trim($value))];
37+
})
38+
->all();
2939
}
3040

3141
if (!empty($data['data'])) {
32-
// TODO: handle JSON payload
33-
parse_str(implode('&', $data['data']), $request->data);
42+
if (count($data['data']) === 1 && Str::startsWith($data['data'][0], '{')) {
43+
$request->data = $data['data'];
44+
$request->jsonData = true;
45+
} else {
46+
parse_str(implode('&', $data['data']), $request->data);
47+
array_walk_recursive($request->data, function (&$value) {
48+
if (is_string($value)) {
49+
$value = self::convertDataType($value);
50+
}
51+
});
52+
}
3453
}
3554

3655
if (!empty($data['fields'])) {
@@ -51,6 +70,11 @@ public function headers(): array
5170
return $this->headers;
5271
}
5372

73+
public function isJsonData(): bool
74+
{
75+
return $this->jsonData;
76+
}
77+
5478
public function method(): string
5579
{
5680
return $this->method;
@@ -61,8 +85,13 @@ public function url(): string
6185
return $this->url;
6286
}
6387

64-
public function isMultipartFormData()
88+
public function isMultipartFormData(): bool
6589
{
6690
return $this->multipartFormData;
6791
}
92+
93+
private static function convertDataType(string $value)
94+
{
95+
return preg_match('/^[1-9]\d*$/', $value) ? intval($value) : $value;
96+
}
6897
}

app/Support/HttpCall.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ private static function generateOptions(Request $request): string
2121
{
2222
$options = [];
2323

24+
if (!empty($request->data()) && $request->method() !== 'GET') {
25+
$options[] = $request->isJsonData() ? 'withBody(\'' . $request->data()[0] . '\')'
26+
: 'asForm()';
27+
}
28+
2429
if ($request->headers()) {
2530
// TODO: what about headers that have Http helper methods, for example: `acceptJson`
2631
$options[] = 'withHeaders(' . self::prettyPrintArray($request->headers()) . ')';
@@ -35,7 +40,7 @@ private static function generateOptions(Request $request): string
3540

3641
private static function generateRequest(Request $request): string
3742
{
38-
if (empty($request->data())) {
43+
if (empty($request->data()) || $request->isJsonData()) {
3944
return "'" . $request->url() . "'";
4045
}
4146

tests/Feature/Console/Commands/CurlCommandTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ public function curlCommandFixtures()
2626
'GET request' => ['basic-get'],
2727
'POST request' => ['basic-post'],
2828
'POST request with multiple data' => ['post-with-multiple-data'],
29+
'POST request with JSON data' => ['post-json'],
30+
'GET request with headers' => ['with-headers'],
2931
];
3032
}
3133
}

tests/fixtures/basic-post.out

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
Http::post('https://example.com', [
2-
'foo' => 'bar',
3-
]);
1+
Http::asForm()
2+
->post('https://example.com', [
3+
'foo' => 'bar',
4+
]);

tests/fixtures/post-json.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
curl -X POST -d '{"foo":"bar"}' https://example.com

tests/fixtures/post-json.out

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Http::withBody('{"foo":"bar"}')
2+
->post('https://example.com');
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
Http::post('https://example.com', [
2-
'foo' => 'bar',
3-
'rho' => [1, 2],
4-
'baz' => 'qui',
5-
]);
1+
Http::asForm()
2+
->post('https://example.com', [
3+
'foo' => 'bar',
4+
'rho' => [1, 2],
5+
'baz' => 'qui',
6+
]);

tests/fixtures/with-headers.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
curl -H "X-Header-One: 1" -H 'X-Header-Two: 2' https://example.com

tests/fixtures/with-headers.out

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Http::withHeaders([
2+
'X-Header-One' => 1,
3+
'X-Header-Two' => 2,
4+
])
5+
->get('https://example.com');

0 commit comments

Comments
 (0)