Guzzle http 和 Laravel Http post 方法替换为 get 方法。

有关该问题的一些信息:

  • 我使用 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

http://img4.mukewang.com/639535d3000132cb03040152.jpg

该方法是 GET 而不是 POST

我的路线:

http://img4.mukewang.com/639535de0001510303300024.jpg

我使用 Postman 和 curl。有用!该死。



慕尼黑5688855
浏览 185回答 1
1回答

交互式爱情

问题是......你应该使用 https:// instad of http://
打开App,查看更多内容
随时随地看视频慕课网APP