Skip to content

Commit 39931c7

Browse files
committed
fix: json payload
Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
1 parent ec691b9 commit 39931c7

1 file changed

Lines changed: 44 additions & 2 deletions

File tree

src/NextcloudApiContext.php

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ public function sendRequest(string $verb, string $url, $body = null, array $head
213213

214214
try {
215215
list($fullUrl, $options) = $this->beforeRequest($fullUrl, $options);
216+
$options = $this->normalizePayloadForRequest($verb, $options);
216217
$this->requestOptions = $options;
217218
$this->response = $client->{$verb}($fullUrl, $options);
218219
} catch (ClientException $ex) {
@@ -222,6 +223,42 @@ public function sendRequest(string $verb, string $url, $body = null, array $head
222223
}
223224
}
224225

226+
private function normalizePayloadForRequest(string $verb, array $options): array {
227+
if (empty($options['form_params'])) {
228+
return $options;
229+
}
230+
231+
$writeVerbs = ['post', 'put', 'patch'];
232+
if (!in_array(strtolower($verb), $writeVerbs, true)) {
233+
return $options;
234+
}
235+
236+
$hasComplexPayload = false;
237+
foreach ($options['form_params'] as $value) {
238+
if (is_array($value) || $value instanceof \stdClass) {
239+
$hasComplexPayload = true;
240+
break;
241+
}
242+
}
243+
244+
if (!$hasComplexPayload) {
245+
return $options;
246+
}
247+
248+
$encoded = json_encode($options['form_params']);
249+
Assert::assertIsString($encoded);
250+
$decoded = json_decode($encoded, true);
251+
Assert::assertIsArray($decoded);
252+
253+
$options['json'] = $decoded;
254+
unset($options['form_params']);
255+
if (!isset($options['headers']['Content-Type'])) {
256+
$options['headers']['Content-Type'] = 'application/json';
257+
}
258+
259+
return $options;
260+
}
261+
225262
#[Given('/^set the custom http header "([^"]*)" with "([^"]*)" as value to next request$/')]
226263
public function setTheCustomHttpHeaderAsValueToNextRequest(string $header, string $value):void {
227264
if (empty($value)) {
@@ -239,11 +276,15 @@ protected function beforeRequest(string $fullUrl, array $options): array {
239276

240277
protected function decodeIfIsJsonString(array $list): array {
241278
foreach ($list as $key => $value) {
242-
if ($this->isJson($value)) {
243-
$list[$key] = json_decode($value);
279+
if (!is_string($value)) {
280+
continue;
244281
}
245282
if (str_starts_with($value, '(string)')) {
246283
$list[$key] = substr($value, strlen('(string)'));
284+
continue;
285+
}
286+
if ($this->isJson($value)) {
287+
$list[$key] = json_decode($value);
247288
}
248289
}
249290
return $list;
@@ -447,6 +488,7 @@ public function setAppConfig(string $appId, TableNode $formData): void {
447488
protected function parseFormParams(array $options): array {
448489
if (!empty($options['form_params'])) {
449490
$this->parseTextRcursive($options['form_params']);
491+
$options['form_params'] = $this->decodeIfIsJsonString($options['form_params']);
450492
}
451493
return $options;
452494
}

0 commit comments

Comments
 (0)