Skip to content

Commit c8a3273

Browse files
committed
Handle automatic POST requests
1 parent d8ca433 commit c8a3273

13 files changed

+42
-10
lines changed

app/Models/Request.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class Request
2525
private function __construct($url, $method)
2626
{
2727
$this->url = $url;
28-
$this->method = strtoupper($method);
28+
$this->method = Str::upper($method);
2929
}
3030

3131
public static function create(array $data): self
@@ -56,6 +56,10 @@ public static function create(array $data): self
5656
$request->multipartFormData = true;
5757
}
5858

59+
if (!empty($request->data) && $request->method === 'GET') {
60+
$request->method = 'POST';
61+
}
62+
5963
if ($data['user']) {
6064
[$request->username, $request->password] = explode(':', $data['user'], 2);
6165
}

app/Support/HttpCall.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,17 @@ private static function collapseHelpers(array $headers, array &$options): array
3939
->all();
4040
}
4141

42-
private static function filterHeaders(array $headers): array
42+
private static function filterHeaders(Request $request): array
4343
{
44-
return collect($headers)
45-
->reject(function ($value, $header) {
46-
// has header to match multipart or ascii form data
44+
return collect($request->headers())
45+
->reject(function ($value, $header) use ($request) {
46+
if ($request->data() && $header === 'Content-Type') {
47+
if ($request->isMultipartFormData() && Str::lower($value) === 'multipart/form-data') {
48+
return true;
49+
} elseif (Str::lower($value) === 'application/x-www-form-urlencoded') {
50+
return true;
51+
}
52+
}
4753

4854
if ($header === 'Content-Type' && Str::lower($value) === 'application/json') {
4955
return true;
@@ -68,7 +74,7 @@ private static function generateOptions(Request $request): string
6874
}
6975
}
7076

71-
$headers = self::filterHeaders($request->headers());
77+
$headers = self::filterHeaders($request);
7278
$headers = self::collapseHelpers($headers, $options);
7379

7480
if ($request->hasUsernameOrPassword()) {

tests/Feature/Console/Commands/CurlCommandTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ public function curlCommandFixtures()
2828
'POST request with data' => ['post-with-data'],
2929
'POST request with JSON data' => ['post-json'],
3030
'POST request with multipart/form-data' => ['post-with-form-data'],
31+
'Request with data defaults to POST' => ['request-with-data'],
32+
'Request with form fields defaults to POST' => ['request-with-form-data'],
33+
'Request with collapsable headers' => ['with-collapsable-headers'],
34+
'PUT request with data' => ['put-with-data'],
3135
'GET request with headers' => ['with-headers'],
3236
'Mailgun example request' => ['mailgun-example'],
3337
'Digital Ocean example request' => ['digital-ocean-example'],
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
curl -H 'Accept: application/json' -d 'format=json' https://example.com
1+
curl -H 'Accept: application/json' https://example.com
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
11
Http::acceptJson()
2-
->get('https://example.com' [
3-
'format' => 'json',
4-
]);
2+
->get('https://example.com');

tests/fixtures/put-with-data.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
curl -X PUT -d 'foo=bar&baz=qui' https://example.com

tests/fixtures/put-with-data.out

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

tests/fixtures/request-with-data.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
curl -H 'Content-Type: application/x-www-form-urlencoded' -d 'foo=bar&baz=qui' https://example.com

tests/fixtures/request-with-data.out

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Http::asForm()
2+
->post('https://example.com', [
3+
'foo' => 'bar',
4+
'baz' => 'qui',
5+
]);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
curl -H 'Content-Type: multipart/form-data' -F 'foo=bar' https://example.com

0 commit comments

Comments
 (0)