diff options
| author | liv | 2025-03-25 20:34:50 +0000 |
|---|---|---|
| committer | liv | 2025-03-25 20:34:50 +0000 |
| commit | 3e0141d754b48056a9b395eb2304c2409d7ffcde (patch) | |
| tree | 33a520b6a064c62dd86a047c02a61211938c094e /misc/send_request_authenticated.php | |
| parent | 60072703467c93935fae239813881474d0c92fd0 (diff) | |
| parent | 0a83d6a3f27b90355d450ce6b8390303c169a47f (diff) | |
chore: merge winter/digitigrade:main into main
Diffstat (limited to 'misc/send_request_authenticated.php')
| -rw-r--r-- | misc/send_request_authenticated.php | 21 |
1 files changed, 17 insertions, 4 deletions
diff --git a/misc/send_request_authenticated.php b/misc/send_request_authenticated.php index 6e74cac..842334c 100644 --- a/misc/send_request_authenticated.php +++ b/misc/send_request_authenticated.php @@ -17,7 +17,8 @@ function send_request_authenticated( bool $dontLookUpInstance = false, string $method = 'GET', ?Actor $actingAs = null, - ?string $body = null + ?string $body = null, + ?string $contentType = null ) { if (!str_starts_with($uri, 'https://')) { throw new RuntimeException('refusing to fetch a non-https uri'); @@ -33,10 +34,18 @@ function send_request_authenticated( $instance = Instance::findByHostname($remoteHost); } + if ($body != null && $method != 'GET') { + $contentType ??= 'application/x-www-form-urlencoded'; + } + if (!isset($instance->auth->outboundToken) || $instance->auth->outboundToken == null) { // hopefully we can make the request anyway? - $context = stream_context_create(['http' => ['method' => $method, 'content' => $body]]); - $resp = file_get_contents($uri); + $context = stream_context_create(['http' => [ + 'method' => $method, + 'content' => $body, + 'header' => isset($contentType) ? "Content-Type: $contentType" : null + ]]); + $resp = file_get_contents($uri, context: $context); // $http_response_header just poofs into existence . // i don't like this api. i should probably use curl instead. hmm // TODO: use curl instead of file_get_contents @@ -56,9 +65,13 @@ function send_request_authenticated( if ($actingAs != null) { $header .= "\nX-PawPub-Actor: $actingAs->uri"; } + if ($contentType != null) { + $header .= "\nContent-Type: $contentType"; + } $context = stream_context_create(['http' => [ 'method' => $method, - 'header' => $header + 'header' => $header, + 'content' => $body ]]); $result = new stdClass(); $result->body = file_get_contents($uri, context: $context); |
