Guzzle http 和 Laravel Http post 方法替换为 get 方法。 405 方法不允许
Guzzle http and Laravel Http post method replace with get method. 405 Method Not Allowed
关于问题的一些信息:
- 我使用 POST 方法创建 guzzle 客户端,但它替换为 GET 方法。
- 我尝试在 Laravel 7.x 上使用
Illuminate\Support\Facades\Http
但出现相同的错误。
- API 服务器是 Django Rest Framework。
- 我收到 405 Method Not Allowed 响应。
我使用 guzzle 的代码:
public static function post($url, $headers, $body)
{
$client = new Client([
'headers' => $headers
]);
$response = collect();
try {
$response->code = 200;
$response->body = json_decode($client->post($url, $body)->getBody()->getContents());
} catch (RequestException $e) {
$response->code = $e->getCode();
$response->body = $e->getResponse() ? $e->getResponse()->getBody()->getContents() : $e->getMessage();
$response->url = $url;
}
return $response;
}
public static function posting()
{
$url = 'http://xapi-staging.uii.ac.id';
$headers = [
'Content-Type' => 'application/json',
'Authorization' => ""
];
$body = [
'form_params' => [
"parameter" => "value"
]
];
return static::post($url, $headers, $body);
}
dd(static::posting());
我同时使用 form_params 和 json 但仍然出现相同的错误。
那我用LaravelIlluminate\Support\Facades\Http
Http::withHeaders([
'Authorization' => ""
])->post('http://xapi-staging.uii.ac.id', [
"parameter" => "value",
]);
请参阅 Illuminate\Support\Facades\Http
文档,网址为:https://laravel.com/docs/7.x/http-client#request-data
结果使用 Laravel Illuminate\Support\Facades\Http
方法是GET而不是POST
我的路线:
我使用 Postman 和 curl。有用!该死的
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://xapi-staging.uii.ac.id",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS =>"{\n \"parameter\": \"value"",
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.ey..",
"Content-Type: text/plain"
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
问题是...您应该使用 https:// 而不是 http://
关于问题的一些信息:
- 我使用 POST 方法创建 guzzle 客户端,但它替换为 GET 方法。
- 我尝试在 Laravel 7.x 上使用
Illuminate\Support\Facades\Http
但出现相同的错误。 - API 服务器是 Django Rest Framework。
- 我收到 405 Method Not Allowed 响应。
我使用 guzzle 的代码:
public static function post($url, $headers, $body)
{
$client = new Client([
'headers' => $headers
]);
$response = collect();
try {
$response->code = 200;
$response->body = json_decode($client->post($url, $body)->getBody()->getContents());
} catch (RequestException $e) {
$response->code = $e->getCode();
$response->body = $e->getResponse() ? $e->getResponse()->getBody()->getContents() : $e->getMessage();
$response->url = $url;
}
return $response;
}
public static function posting()
{
$url = 'http://xapi-staging.uii.ac.id';
$headers = [
'Content-Type' => 'application/json',
'Authorization' => ""
];
$body = [
'form_params' => [
"parameter" => "value"
]
];
return static::post($url, $headers, $body);
}
dd(static::posting());
我同时使用 form_params 和 json 但仍然出现相同的错误。
那我用LaravelIlluminate\Support\Facades\Http
Http::withHeaders([
'Authorization' => ""
])->post('http://xapi-staging.uii.ac.id', [
"parameter" => "value",
]);
请参阅 Illuminate\Support\Facades\Http
文档,网址为:https://laravel.com/docs/7.x/http-client#request-data
结果使用 Laravel Illuminate\Support\Facades\Http
方法是GET而不是POST
我的路线:
我使用 Postman 和 curl。有用!该死的
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://xapi-staging.uii.ac.id",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS =>"{\n \"parameter\": \"value"",
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.ey..",
"Content-Type: text/plain"
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
问题是...您应该使用 https:// 而不是 http://