简体   繁体   English

Laravel Post 请求空请求参数

[英]Laravel Post Request empty Request parameters

When I post to my API endpoint with postman or my application the request parameter is an empty array.当我使用邮递员或我的应用程序发布到我的 API 端点时, request参数是一个空数组。

With other controllers post requests work fine and the request param gets handled well.使用其他控制器发布请求工作正常并且request参数得到很好的处理。 Only with my order controller does my post body not get processed.只有使用我的订单控制器,我的帖子正文才不会得到处理。

I haven't made any changes and reverting to an older commit also did not help.我没有进行任何更改,恢复到较旧的提交也无济于事。

I'm at the point where I have no clue why it doesn't accept the JSON body.我现在不知道为什么它不接受 JSON 正文。

Controller控制器

public function createOrder(Request $request)
{
    return $request->product_id;
}

POST body | POST 正文| Raw JSON原始 JSON

{
    "product_id": 4
}

Response response is an empty array []"响应响应是一个空数组 []"

I've tried to print the $request and I got this as response我试图打印$request并且我得到了这个作为响应

POST /api/order/post HTTP/1.1
Accept:          application/json
Accept-Encoding: gzip, deflate, br
Authorization:   Bearer.{token}
Cache-Control:   no-cache
Connection:      keep-alive
Content-Length:  20
Content-Type:    
Cookie:          XSRF-TOKEN={Token}%3D%3D; laravel_session=%3D%3D
Host:            backend.host
User-Agent:      PostmanRuntime/7.22.0
Cookie: XSRF-TOKEN==={token}; laravel_session={Token}

{
    "product_id": 4
}

My post body is visible then so I'm confused why it's not showing我的帖子正文是可见的,所以我很困惑为什么它没有显示

What I expect and what I get我的期望和我得到的

I expect the request to return the value of my JSON body我希望请求返回我的 JSON 正文的值

what is happening nothing is returned发生了什么 没有返回

Note: I've recently added passport, but it was working fine last time I used this endpoint after adding laravel注意:我最近添加了通行证,但上次在添加laravel后使用此端点时效果laravel

Working controller工作控制器

public function store(Request $request)
{
    return $request;
}

Api routes Api 路由

Route::post('order/create', 'OrderController@createOrder');
Route::post('product/create', 'ProductController@store');

If I do echo $request in controller i see that there are data in the request, but I think that format is not good:如果我在控制器中echo $request我看到echo $request中有数据,但我认为这种格式不好:

POST /api/messages HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate, br
Authorization: Bearer 5|ibun75O2SNnZoT2W5Rk0KQox70wBxKN4xIIrq0sQ
Cache-Control: no-cache
Connection: keep-alive
Content-Length: 522
Content-Type: application/json
Host: localhost:8000
User-Agent: PostmanRuntime/7.26.10

{
    "text" : "Molestiae dolor non eaque aut sapiente maiores. Aut voluptates nesciunt. Cum distinctio quisquam qui vitae eos
asperiores.

Nihil iste quia. Voluptate deleniti ipsam voluptas alias similique doloremque. Ratione alias iste voluptatem quis. Quis
quia quo sequi minus quod voluptate cum similique dolor.

Maxime enim aut. Eius ea doloribus. Quaerat dolor libero ipsum. Delectus atque esse ipsam est.",
    "imageLink" : "http://placeimg.com/640/480/abstract",
    "typeID" : "232",
    "answerTypeID" : "919"
}

Add Content-Type:application/json to make PHP understand that it is receiving a JSON.添加Content-Type:application/json使 PHP 知道它正在接收 JSON。 Currently, your Content-type looks empty.目前,您的Content-type看起来是空的。 Also make sure the CSRF token you pass is correct.还要确保您传递的 CSRF 令牌是正确的。

Check that your request has:检查您的请求是否具有:

  1. Add the header Accept with value application/json .添加带有值application/json的标题Accept
  2. If body is raw , make sure the type is JSON .如果 body 是raw ,请确保类型是JSON
  3. If body is raw , make sure the content is a valid JSON and DO NOT include comments in JSON.如果 body 是raw ,请确保内容是有效的 JSON 并且不要在 JSON 中包含注释

For example:例如:

This works: raw body with type JSON:这有效: JSON 类型的原始主体:

{
    "email": "{{email}}",
    "password": "{{password}}"
}

This DOES NOT work: raw body even with type JSON:这不起作用:即使是 JSON 类型的原始正文:

{
    "email": "{{email}}", // Eg. superadmin@example.com <-- This invalidates the JSON
    "password": "{{password}}" // Eg. password <-- This invalidates the JSON
}

Notice that in Postman, the JSON comments are visually/syntactically rendered as valid part of the JSON, but is actually invalid.请注意,在 Postman 中,JSON 注释在视觉/语法上呈现为 JSON 的有效部分,但实际上是无效的。

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

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