Skip to content

Commit

Permalink
improve multipart reader return stdClass in case no file uploads are …
Browse files Browse the repository at this point in the history
…provided
  • Loading branch information
chriskapp committed Apr 6, 2024
1 parent 2da77c4 commit d455b5e
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/Reader/Multipart.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use PSX\Data\Multipart\Body;
use PSX\Data\Multipart\File;
use PSX\Data\ReaderAbstract;
use PSX\Data\Util\CurveArray;
use PSX\Http\MediaType;

/**
Expand Down Expand Up @@ -53,11 +54,18 @@ public function read(string $data): mixed
}
}

foreach ($this->post as $name => $value) {
$multipart->addPart($name, $value);
}
if ($multipart->hasFile()) {
foreach ($this->post as $name => $value) {
$multipart->addPart($name, $value);
}

return $multipart;
return $multipart;
} else {
// we use the multipart body only in case there are file uploads, otherwise we return a stdClass similar to
// the form reader, this is needed since some HTTP clients like RestSharp always uses a multipart content
// type containing the form values
return CurveArray::objectify($this->post);
}
}

public function isContentTypeSupported(MediaType $contentType): bool
Expand Down

0 comments on commit d455b5e

Please sign in to comment.