laravel API 返回 400 Bad Request 响应

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39934948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:34:53  来源:igfitidea点击:

API returning 400 Bad Request response

apilaravellaravel-5.2

提问by katie hudson

I have built an API and an application that uses that API. Everything was working but now, for some reason, I get a 400 Bad Request response. I am not sure if I changed something in the code so I wanted to double check it was correct.

我已经构建了一个 API 和一个使用该 API 的应用程序。一切正常,但现在,出于某种原因,我收到了 400 Bad Request 响应。我不确定我是否更改了代码中的某些内容,因此我想仔细检查它是否正确。

So my API call is this

所以我的 API 调用是这样的

$client = new GuzzleHttp\Client();

$jsonData = json_encode($data);

$req = $client->request('POST', 'https://someurl.com/api/v1/createProject', [
    'body' => $jsonData,
    'headers' => [
        'Content-Type' => 'application/json',
        'Content-Length' => strlen($jsonData),
    ]
]);

$output = $req->getBody()->getContents();

The API has a route set up correctly which uses post. The function it calls is correct, and I have changed it for testing to simply return

API 有一个正确设置的路由,它使用 post。它调用的函数是正确的,为了测试我已经将其更改为简单返回

return response()->json(["Success", 200]);

When I test the API out within Postman, I can see that Success is returned. When I test the API within the other application I have built, I dont even see a POST request within the console, I am just displayed a Laravel error 400 Bad Request.

当我在 Postman 中测试 API 时,我可以看到返回了 Success。当我在我构建的其他应用程序中测试 API 时,我什至没有在控制台中看到 POST 请求,我只是显示了 Laravel 错误 400 错误请求。

What could be the cause of this issue?

导致此问题的原因可能是什么?

Thanks

谢谢

Update

更新

I have changed the request to this

我已将请求更改为此

$data= json_encode($data);

$req = $client->post('https://someurl.com/api/v1/createProject', [
    'body' => $data
]);

If I output $dataafter it has been encoded, I get something like this

如果我$data在编码后输出,我会得到这样的东西

{
    "projectName":"New Project",
    "clientName":"Test Client",
}

Within the controller function of the API that is being called, I simply do

在被调用的 API 的控制器函数中,我只是做

return response()->json(['name' => $request->input('clientName')]);

The 400 error has now gone, but I now get null returned to me

400 错误现已消失,但我现在得到 null 返回给我

{#326 ▼
  +"name": null
}

Request is being injected into the function as it should be. Should I be returning the data in a different way?

请求被注入到函数中,因为它应该是。我应该以不同的方式返回数据吗?

Thanks

谢谢

回答by Kyslik

Probably you did $ composer updateand Guzzle updated.

可能你做到了$ composer update,Guzzle 更新了。

So if you are using newest Guzzle (guzzlehttp/guzzle (6.2.2)) you do POSTrequest:

因此,如果您使用的是最新的 Guzzle (guzzlehttp/guzzle (6.2.2)),则执行POST请求:

$client = new GuzzleHttp\Client();

$data = ['name' => 'Agent Smith'];

$response = $client->post('http://example.dev/neo', [
    'json' => $data
]);

You do notneed to specify headers.

没有需要指定头。

To read response you do following:

要阅读响应,请执行以下操作:

$json_response = json_decode($response->getBody());


My full example (in routes file web.phproutes.php)

我完整的例子(在routes文件web.phproutes.php文件

Route::get('smith', function () {
    $client = new GuzzleHttp\Client();

    $data = ['name' => 'Agent Smith']; 

    $response = $client->post('http://example.dev/neo', [
        'json' => $data,
    ]);

    $code = $response->getStatusCode();
    $result = json_decode($response->getBody());

    dd($code, $result);
});

    Route::post('neo', function (\Illuminate\Http\Request $request) {
        return response()->json(['name' => $request->input('name')]);
    });

or you could use following (shortened), but code above is "shorter"

或者您可以使用以下(缩短),但上面的代码“更短”

$json_data = json_encode(['name' => 'Agent Smith']);

$response = $client->post('http://example.dev/neo', [
    'body' => $json_data,
    'headers' => [
        'Content-Type' => 'application/json',
        'Content-Length' => strlen($json_data),
    ]
]);

note: If you are running PHP5.6, change always_populate_raw_post_datato -1(or uncomment the line) in php.ini and restart your server. Read more here.

:如果您运行的PHP5.6,变化always_populate_raw_post_data-1(或取消对该行)在php.ini并重新启动服务器。在这里阅读更多。

回答by DragonFire

In my case I was using public IP address in BASE_URL while I should have been using the private IP. From mac you can get your IP by going into system preferences -> network.

就我而言,我在 BASE_URL 中使用公共 IP 地址,而我应该使用私有 IP。在 Mac 上,您可以通过进入系统首选项 -> 网络来获取您的 IP。

This is with Android + Laravel (API)

这是 Android + Laravel (API)