简体   繁体   English

格式错误的 HTTP 请求 Laravel

[英]Malformed HTTP Request Laravel

I am building a Laravel back-end API.我正在构建一个 Laravel 后端 API。 The API has been implemented and I have tested all the necessary methods and routes. API已经实现,我已经测试了所有必要的方法和路线。 I am now busy creating some test-requests using Postman however I keep getting a 'Malformed HTTP Request' Error in Laravel.我现在正忙于使用 Postman 创建一些测试请求,但是我在 Laravel 中不断收到“格式错误的 HTTP 请求”错误。

The server side verification is done using Laravel Passport, below is the generated HTTP code from Postman服务器端验证使用 Laravel Passport 完成,下面是从 Postman 生成的 HTTP 代码

POST /api/my_data_url HTTP/1.1
Host: 127.0.0.1:8000
Accept: application/json
Authorization: Bearer oauth_key_goes_here
Content-Type: application/json

{
    "value_1": 123,
    "value_2": 123,
    "value_3": 123
}

I have had a look at the JSON:API standard however it is not necessary for the purposes of this app, it would be overkill as I am not creating a public API.我已经查看了 JSON:API 标准,但是对于这个应用程序的目的来说这不是必需的,因为我没有创建一个公共的 API,这将是矫枉过正。 This is my own app I will be using.这是我将使用的我自己的应用程序。 On the controller side I merely want it to echo so it looks like this currently:在 controller 方面,我只想让它回显,所以目前看起来像这样:

/**
* Store a newly created resource in storage.
*
* @param  \Illuminate\Http\Request  $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
  {
     return response()->json($request->json()->all(), 200);
  }

My end goal is to merely pass some sensor data to a Laravel API and use the appropriate controller method to verify and save it to a database.我的最终目标只是将一些传感器数据传递给 Laravel API 并使用适当的 controller 方法进行验证并将其保存到数据库中。 What am I missing, it feels like I am making a stupid mistake?我错过了什么,感觉就像我犯了一个愚蠢的错误?

PS I know that there is another question with a very similar problem however it doesn't appear as if that question has a solution that will help me. PS我知道还有另一个问题非常相似,但是似乎该问题没有可以帮助我的解决方案。

I thought my original answer was detailed enough as it is merely a configuration error.我认为我的原始答案足够详细,因为它只是一个配置错误。 I will attempt a second answer that is more detailed here.我将尝试在这里更详细的第二个答案。 Please note I am using the Linux-based desktop application, I cannot comment on other versions as I do not use them.请注意,我使用的是基于 Linux 的桌面应用程序,我无法评论其他版本,因为我不使用它们。 My Laravel application was also built using Laravel 6.我的 Laravel 应用程序也是使用 Laravel 6 构建的。

My error was caused by incorrect request authorization settings.我的错误是由不正确的请求授权设置引起的。 The steps below allowed me to correct the issue.下面的步骤使我能够纠正这个问题。

After you have opened the Postman application do the following:打开 Postman 应用程序后,请执行以下操作:

  1. Create a request in Postman by clicking on the Create a request label on the right-hand side of the start page of the desktop application通过单击桌面应用程序起始页右侧的Create a request label 在 Postman 中创建请求
  2. You will now be in the request creation tab.您现在将位于请求创建选项卡中。 Within this, there are multiple tabs to allow you to configure parts of your request.在此范围内,有多个选项卡可让您配置部分请求。 eg Params , Authorization etc.例如ParamsAuthorization等。
  3. Navigate to the Authorization tab and change the Type dropdown to match the authorization schema your Laravel application uses.导航到Authorization选项卡并更改类型下拉列表以匹配您的 Laravel 应用程序使用的授权模式。

These were the steps I took to fix the issue.这些是我为解决问题而采取的步骤。

I eventually figured out what it was months later.几个月后,我终于弄清楚了它是什么。 My Postman configuration was incorrect.我的 Postman 配置不正确。 In the Postman settings I had configured the incorrect HTTP authentication scheme, so all my requests were "Malformed" because they contained a different security mechanism than that which Laravel knew.在 Postman 设置中,我配置了不正确的 HTTP 身份验证方案,因此我的所有请求都是“格式错误的”,因为它们包含的安全机制与 Laravel 知道的不同。 Once I corrected this and changed it from what it was to OAuth2 with a bearer token, my requests came through beautifully!一旦我纠正了这个问题并将其从原来的状态更改为带有不记名令牌的 OAuth2,我的请求就顺利通过了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM