Skip to content

Commit 424af28

Browse files
authored
Merge pull request #146 from DotNetSimon/raw_body_webhook_option
Webhook option to allow sending a raw body instead of array -> json.
2 parents 58ad35c + dd29d11 commit 424af28

File tree

4 files changed

+54
-2
lines changed

4 files changed

+54
-2
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,19 @@ WebhookCall::create()
349349
```
350350
or activate the `throw_exception_on_failure` global option of the `webhook-server` config file.
351351

352+
### Sending raw string body instead of JSON
353+
354+
By default, all webhooks will transform the payload into JSON. Instead of sending JSON, you can send any string by using the `sendRawBody(string $body)` option instead.
355+
356+
Due to type mismatch in the Signer API, it is currently not support to sign raw data requests
357+
```php
358+
WebhookCall::create()
359+
->sendRawBody("<root>someXMLContent</root>")
360+
->doNotSign()
361+
...
362+
->dispatch();
363+
```
364+
352365
### Events
353366

354367
The package fires these events:

src/CallWebhookJob.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,16 @@ class CallWebhookJob implements ShouldQueue
4646
/** @var string|null */
4747
public $queue = null;
4848

49-
public array $payload = [];
49+
public array|string $payload = [];
5050

5151
public array $meta = [];
5252

5353
public array $tags = [];
5454

5555
public string $uuid = '';
5656

57+
public string $outputType = "JSON";
58+
5759
protected ?Response $response = null;
5860

5961
protected ?string $errorType = null;
@@ -69,7 +71,7 @@ public function handle()
6971
try {
7072
$body = strtoupper($this->httpVerb) === 'GET'
7173
? ['query' => $this->payload]
72-
: ['body' => json_encode($this->payload)];
74+
: ['body' => $this->generateBody()];
7375

7476
$this->response = $this->createRequest($body);
7577

@@ -162,4 +164,12 @@ private function dispatchEvent(string $eventClass)
162164
$this->transferStats
163165
));
164166
}
167+
168+
private function generateBody(): string
169+
{
170+
return match ($this->outputType) {
171+
"RAW" => $this->payload,
172+
default => json_encode($this->payload),
173+
};
174+
}
165175
}

src/WebhookCall.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,14 @@ public function dispatchSyncUnless($condition): void
255255
$this->dispatchSyncIf(! $condition);
256256
}
257257

258+
public function sendRawBody(string $body): self
259+
{
260+
$this->callWebhookJob->payload = $body;
261+
$this->callWebhookJob->outputType = "RAW";
262+
263+
return $this;
264+
}
265+
258266
protected function prepareForDispatch(): void
259267
{
260268
if (! $this->callWebhookJob->webhookUrl) {

tests/CallWebhookJobTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,3 +300,24 @@ function baseGetRequest(array $overrides = []): array
300300
return true;
301301
});
302302
});
303+
304+
it('send raw body data if rawBody is set', function () {
305+
$testBody = "<xml>anotherOption</xml>";
306+
WebhookCall::create()
307+
->url('https://example.com/webhooks')
308+
->useSecret('abc')
309+
->sendRawBody($testBody)
310+
->doNotSign()
311+
->dispatch();
312+
313+
$baseRequest = baseRequest();
314+
315+
$baseRequest['options']['body'] = $testBody;
316+
unset($baseRequest['options']['headers']['Signature']);
317+
318+
artisan('queue:work --once');
319+
320+
$this
321+
->testClient
322+
->assertRequestsMade([$baseRequest]);
323+
});

0 commit comments

Comments
 (0)