Skip to content

Commit 20bab4d

Browse files
Support --data-raw (#18)
1 parent c011a2e commit 20bab4d

File tree

8 files changed

+49
-14
lines changed

8 files changed

+49
-14
lines changed

src/Console/Commands/CurlCommand.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
class CurlCommand extends Command
99
{
10-
protected $signature = 'shift:curl {--X|request=} {--G|get} {--H|header=*} {--d|data=*} {--data-urlencode=*} {--F|form=*} {--digest} {--basic} {--connect-timeout=} {--max-timeout=} {--retry=} {--s|silent} {--u|user=} {--L|location} {--compressed} {--insecure} {url}';
10+
protected $signature = 'shift:curl {--X|request=} {--G|get} {--H|header=*} {--d|data=*} {--data-urlencode=*} {--data-raw=*} {--F|form=*} {--digest} {--basic} {--connect-timeout=} {--max-timeout=} {--retry=} {--s|silent} {--u|user=} {--L|location} {--compressed} {--insecure} {url}';
1111

1212
protected $description = 'Convert a UNIX curl request to an HTTP Client request';
1313

@@ -30,6 +30,7 @@ private function gatherOptions()
3030
'url' => $this->argument('url'),
3131
'headers' => $this->option('header'),
3232
'data' => $this->option('data'),
33+
'rawData' => $this->option('data-raw'),
3334
'dataUrlEncode' => $this->option('data-urlencode'),
3435
'fields' => $this->option('form'),
3536
'digest' => $this->option('digest'),

src/Models/Request.php

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class Request
1414

1515
private array $data = [];
1616

17-
private bool $jsonData = false;
17+
private bool $rawData = false;
1818

1919
private bool $multipartFormData = false;
2020

@@ -52,12 +52,25 @@ public static function create(array $data): self
5252
->all();
5353
}
5454

55+
if (!empty($data['dataUrlEncode'])) {
56+
$request->data = array_merge($request->data, self::parseData($data['dataUrlEncode']));
57+
}
58+
59+
if (!empty($data['rawData'])) {
60+
if (count($data['rawData']) === 1 && empty($data['data']) && empty($data['dataUrlEncode'])) {
61+
$request->data = $data['rawData'];
62+
$request->rawData = true;
63+
} else {
64+
$request->data = array_merge($request->data, self::parseData($data['rawData']));
65+
}
66+
}
67+
5568
if (!empty($data['data'])) {
5669
if (count($data['data']) === 1 && Str::startsWith($data['data'][0], '{')) {
5770
$request->data = $data['data'];
58-
$request->jsonData = true;
71+
$request->rawData = true;
5972
} else {
60-
$request->data = self::parseData($data['data']);
73+
$request->data = array_merge($request->data, self::parseData($data['data']));
6174
}
6275
}
6376

@@ -66,11 +79,7 @@ public static function create(array $data): self
6679
$request->multipartFormData = true;
6780
}
6881

69-
if (!empty($data['dataUrlEncode'])) {
70-
$request->data = self::parseData($data['dataUrlEncode']);
71-
}
72-
73-
if (is_null($data['method']) && (!empty($data['data']) || !empty($data['fields']))) {
82+
if (is_null($data['method']) && (!empty($data['rawData']) || !empty($data['data']) || !empty($data['fields']))) {
7483
$request->method = 'POST';
7584
}
7685

@@ -114,9 +123,9 @@ public function headers(): array
114123
return $this->headers;
115124
}
116125

117-
public function isJsonData(): bool
126+
public function isRawData(): bool
118127
{
119-
return $this->jsonData;
128+
return $this->rawData;
120129
}
121130

122131
public function method(): string

src/Support/HttpCall.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ private static function generateOptions(Request $request): string
6767
if (!empty($request->data()) && $request->method() !== 'GET') {
6868
if ($request->isMultipartFormData()) {
6969
$options[] = 'asMultipart()';
70-
} elseif ($request->isJsonData()) {
71-
$options[] = 'withBody(\'' . $request->data()[0] . '\')';
70+
} elseif ($request->isRawData()) {
71+
$options[] = 'withBody(\'' . current($request->data()) . '\')';
7272
} else {
7373
$options[] = 'asForm()';
7474
}
@@ -102,7 +102,7 @@ private static function generateOptions(Request $request): string
102102

103103
private static function generateRequest(Request $request): string
104104
{
105-
if (empty($request->data()) || $request->isJsonData()) {
105+
if (empty($request->data()) || $request->isRawData()) {
106106
return "'" . $request->url() . "'";
107107
}
108108

tests/Feature/Console/Commands/CurlCommandTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ public function curlCommandFixtures()
4444
'Missing URL scheme' => ['missing-url-scheme'],
4545
'GET request with compressed flag' => ['with-compressed-option'],
4646
'GET request with insecure flag' => ['with-insecure-option'],
47+
'Request with raw data' => ['with-raw-data'],
48+
'POST request with mixed data' => ['raw-data-mixed'],
4749
];
4850
}
4951

tests/fixtures/raw-data-mixed.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
curl --request POST 'https://api.com' --header 'Accept: application/json' --data-raw 'some=data' -d foo=bar

tests/fixtures/raw-data-mixed.out

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

tests/fixtures/with-raw-data.in

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
curl 'https://api.com' --header 'Accept: application/json' --header 'Content-Type: application/json' --data-raw '{
2+
"messages": [
3+
"a",
4+
"b",
5+
"c"
6+
]
7+
}'

tests/fixtures/with-raw-data.out

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Http::withBody('{
2+
"messages": [
3+
"a",
4+
"b",
5+
"c"
6+
]
7+
}')
8+
->acceptJson()
9+
->post('https://api.com');

0 commit comments

Comments
 (0)