【问题标题】:Getting 404 when using Guzzle POST in Laravel - Works in curl在 Laravel 中使用 Guzzle POST 时出现 404 - 在 curl 中工作
【发布时间】:2015-07-18 05:22:45
【问题描述】:

好的,所以我在玩狂饮,似乎我无法让 POST 工作。它适用于 curl cli,但不适用于 laravel guzzle。我是不是做错了什么?

这是我的卷曲(工作正常)

curl -i -d 'locationID=2&firstname=John&middlename=Dee&lastname=Doe&sourceID=2&typeID=2&email=johndoe@yahoo.com&emailtypeID=2&phone=1234567890&phonetypeID=2' http://api.company.com/v1/members

HTTP/1.1 200 OK
Date: Thu, 07 May 2015 14:15:25 GMT
Server: Apache/2.4.7 (Ubuntu)
X-Powered-By: PHP/5.5.9-1ubuntu4.5
Cache-Control: no-cache
Set-Cookie: laravel_session=eyJpdi; 
expires=Thu, 07-May-2015 16:15:26 GMT; 
Max-Age=7200; 
path=/; httponly
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept
Access-Control-Allow-Methods: GET, POST, PUT, DELETE
Content-Length: 59
Content-Type: application/json

这是我的控制器

use GuzzleHttp\Client;

public function postForm(Request $request)
{
    $client = new Client(); 
    $response = $client->post('http://api.company.com/v1/members',
        array(
            'body' => array(
                'firstname' => $firstname,
                'middlename' => $middlename,
                'lastname' => $lastname,
                'locationID' => $locationID,
                'phonetypeID' => $phonetypeID,
                'phone' => $phone,
                'emailtypeID' => $emailtypeID,
                'email' => $email,
                'sourceID' => $sourceID,
                'typeID' => $typeID
            )
        )
    );
    $body = $response->send();
    $content = view('member.create_result')->with('member',json_decode($body, TRUE));
    return Admin::view($content, 'Member Added');
}

我遇到的错误

Client error response [url] http://api.company.com/v1/members [status code] 404 [reason phrase] Not Found

【问题讨论】:

  • 这条路由的方法和路由定义是什么样的?
  • 你是这个意思吗?表单本身正在工作,如果我禁用整个 guzzle 代码,它就会输出数据。 Route::post('/members/add', ['as' => 'members.post', 'uses' => '\App\Http\Controllers\MembersController@postForm', ]);
  • MembersController@postForm 方法也不错。我能想到的另一个想法是在 guzzle 中没有正确设置 Content-Type 标头。这会导致问题吗?
  • 刚刚编辑了我的帖子以包含 postForm 控制器

标签: arrays laravel post curl guzzle


【解决方案1】:

试试这个

$body = [
    'firstname' => $firstname,
    'middlename' => $middlename,
    'lastname' => $lastname,
    'locationID' => $locationID,
    'phonetypeID' => $phonetypeID,
    'phone' => $phone,
    'emailtypeID' => $emailtypeID,
    'email' => $email,
    'sourceID' => $sourceID,
    'typeID' => $typeID
]
$options = [
    'body' => $body
];

$request = $client->createRequest('POST', 'http://api.company.com/v1/members', $options);
$response = $client->send($request);

【讨论】:

  • 同样的错误,我猜这是因为 guzzle 默认将其作为 'content_type' => 'application/json' 发送(根据错误)。我需要通过 x-www-form-urlencoded 发送它。你知道我该如何定义吗?
  • 如果你想改变标题只需添加$options['headers'] = ['Content-Type' => 'x-www-form-urlencoded'];
  • 还是同样的错误。有点奇怪,它适用于带有 x-www-form-urlencoded 和 form-data 的邮递员
  • 你可以用邮递员在请求成功时发布控制台网络选项卡截图吗?
  • 不太确定怎么做,但我在邮递员发帖时成功了
【解决方案2】:

我遇到了同样的问题(在 Postman 中工作的请求和使用 CURL 和 Guzzle 失败)。原来问题是缺少 CORS 标头。我使用这个包将它们添加到我的路线中 - https://github.com/barryvdh/laravel-cors 一切正常:)

也许这会对某人有所帮助,因为我花了太多时间在这件事上绞尽脑汁。

【讨论】:

    猜你喜欢
    • 2020-11-19
    • 2016-02-13
    • 2020-03-28
    • 1970-01-01
    • 2018-03-21
    • 1970-01-01
    • 1970-01-01
    • 2012-07-07
    • 1970-01-01
    相关资源
    最近更新 更多