Skip to content

Commit d654a8d

Browse files
committed
Handle multipart/form data
1 parent ff7a661 commit d654a8d

File tree

7 files changed

+35
-13
lines changed

7 files changed

+35
-13
lines changed

app/Models/Request.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,12 @@ public static function create(array $data): self
4343
$request->data = $data['data'];
4444
$request->jsonData = true;
4545
} 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-
});
46+
$request->data = self::parseData($data['data']);
5247
}
5348
}
5449

5550
if (!empty($data['fields'])) {
56-
$request->data = $data['fields'];
51+
$request->data = self::parseData($data['data']);
5752
$request->multipartFormData = true;
5853
}
5954

@@ -94,4 +89,16 @@ private static function convertDataType(string $value)
9489
{
9590
return preg_match('/^[1-9]\d*$/', $value) ? intval($value) : $value;
9691
}
92+
93+
private static function parseData(array $data): array
94+
{
95+
parse_str(implode('&', $data), $data);
96+
array_walk_recursive($data, function (&$value) {
97+
if (is_string($value)) {
98+
$value = self::convertDataType($value);
99+
}
100+
});
101+
102+
return $data;
103+
}
97104
}

app/Support/HttpCall.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,20 @@ private static function generateOptions(Request $request): string
2222
$options = [];
2323

2424
if (!empty($request->data()) && $request->method() !== 'GET') {
25-
$options[] = $request->isJsonData() ? 'withBody(\'' . $request->data()[0] . '\')'
26-
: 'asForm()';
25+
if ($request->isMultipartFormData()) {
26+
$options[] = 'asMultipart()';
27+
} elseif ($request->isJsonData()) {
28+
$options[] = 'withBody(\'' . $request->data()[0] . '\')';
29+
} else {
30+
$options[] = 'asForm()';
31+
}
2732
}
2833

29-
if ($request->headers()) {
30-
// TODO: what about headers that have Http helper methods, for example: `acceptJson`
31-
$options[] = 'withHeaders(' . self::prettyPrintArray($request->headers()) . ')';
34+
// TODO: filter out headers that have Http helper methods, for example: `acceptJson`
35+
$headers = $request->headers();
36+
37+
if ($headers) {
38+
$options[] = 'withHeaders(' . self::prettyPrintArray($headers) . ')';
3239
}
3340

3441
if (empty($options)) {

tests/Feature/Console/Commands/CurlCommandTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ public function curlCommandFixtures()
2525
return [
2626
'GET request' => ['basic-get'],
2727
'POST request' => ['basic-post'],
28-
'POST request with multiple data' => ['post-with-multiple-data'],
28+
'POST request with data' => ['post-with-data'],
2929
'POST request with JSON data' => ['post-json'],
30+
'POST request with multipart/form-data' => ['post-with-form-data'],
3031
'GET request with headers' => ['with-headers'],
3132
];
3233
}

tests/fixtures/post-with-form-data.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
curl -X POST -F 'foo=bar' -F 'rho[]=1' -F 'baz=qui&rho[]=2' https://example.com
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Http::asMultipart()
2+
->post('https://example.com', [
3+
'foo' => 'bar',
4+
'rho' => [1, 2],
5+
'baz' => 'qui',
6+
]);

0 commit comments

Comments
 (0)