正在检索 JSON 内容
Retrieving JSON content
我是 laravel 的初学者。我在创建 POST API 时遇到问题。
我想使用从其他应用程序 json POST 检索的数据。但它总是 return null 所以我对请求做了一些调查。
当我returndd($request)时,结果是
Illuminate\Http\Request {#43 ▼
#json: Symfony\Component\HttpFoundation\ParameterBag {#35 ▶}
#convertedFiles: []
#userResolver: Closure($guard = null) {#1241 ▼
class: "Illuminate\Auth\AuthServiceProvider"
this: Illuminate\Auth\AuthServiceProvider {#22 …}
use: {▶}
file: "C:\xampp\htdocs\attendance_online\vendor\laravel\framework\src\Illuminate\Auth\AuthServiceProvider.php"
line: "105 to 107"
}
#routeResolver: Closure() {#1246 ▼
class: "Illuminate\Routing\Router"
this: Illuminate\Routing\Router {#26 …}
use: {▶}
file: "C:\xampp\htdocs\attendance_online\vendor\laravel\framework\src\Illuminate\Routing\Router.php"
line: "655 to 657"
}
+attributes: Symfony\Component\HttpFoundation\ParameterBag {#45 ▼
#parameters: []
}
+request: Symfony\Component\HttpFoundation\ParameterBag {#35 ▼
#parameters: []
}
+query: Symfony\Component\HttpFoundation\InputBag {#51 ▼
#parameters: []
}
+server: Symfony\Component\HttpFoundation\ServerBag {#47 ▼
#parameters: array:23 [▶]
}
+files: Symfony\Component\HttpFoundation\FileBag {#48 ▼
#parameters: []
}
+cookies: Symfony\Component\HttpFoundation\InputBag {#46 ▼
#parameters: []
}
+headers: Symfony\Component\HttpFoundation\HeaderBag {#49 ▼
#headers: array:6 [▼
"accept-encoding" => array:1 [▼
0 => "gzip,deflate"
]
"content-type" => array:1 [▼
0 => "application/json"
]
"content-length" => array:1 [▼
0 => "231"
]
"host" => array:1 [▼
0 => "localhost:8000"
]
"connection" => array:1 [▼
0 => "Keep-Alive"
]
"user-agent" => array:1 [▼
0 => "Apache-HttpClient/4.5.5 (Java/12.0.1)"
]
]
#cacheControl: []
}
#content: """
{\n
\t"id_employee" = "4",\n
\t"status" = "1",\n
\t"latitude" = "3.4141414",\n
\t"longitude" = "98.444444",\n
\t"timestamp" = "2020-10-11 09:58:00.0000000",\n
\t"approval" = "1",\n
\t"message" = "this is message",\n
\t"picture_reference" = "this is pic"\n
}
"""
#languages: null
#charsets: null
#encodings: null
#acceptableContentTypes: null
#pathInfo: "/api/Attendance"
#requestUri: "/api/Attendance"
#baseUrl: ""
#basePath: null
#method: "POST"
#format: null
#session: null
#locale: null
#defaultLocale: "en"
-preferredFormat: null
-isHostValid: true
-isForwardedValid: true
-isSafeContentPreferred: null
basePath: ""
format: "html"
}
}
当我 return $request->getcontent() 结果是
{ "id_employee" = "4", "status" = "1", "latitude" = "3.4141414", "longitude" = "98.444444", "timestamp" = "2020-10-11 09:58:00.0000000", "approval" = "1", "message" = "this is message", "picture_reference" = "this is pic" }
但是当我 return $request->all() 是说
<Empty JSON content>
每当我使用 $request->id_employee 或任何其他数据时,它总是 return 为空。当我将参数放在 URL 上时,如“?id_employee=1”,$request->id_employee 将 return 1. 我不想将数据放在URL,那么如何获取 $request 上的所有内容?
编辑:
我已经试过了
$id_employee = $request->id_employee;
$id_employee = $request->input("id_employee")
$id_employee = $request->input("content.id_employee")
并且已经确认 $request->isjson() return 1
你有什么版本的Laravel?您应该能够像这样解码 JSON 内容:
$content = $request->getContent()
$data = json_decode($content, true)
dd(data)
不过请检查 JSON 是否有效 - 您在原始问题中发布的示例 =
通常应该放在 :
的位置! JSON 需要有效,如果请求具有正确的 Content-Type header,那么您对 request->input("employee_id")
的调用应该有效。这并没有让我想知道 JSON 是否是 well-formatted 所以一定要检查一下。
我是 laravel 的初学者。我在创建 POST API 时遇到问题。 我想使用从其他应用程序 json POST 检索的数据。但它总是 return null 所以我对请求做了一些调查。
当我returndd($request)时,结果是
Illuminate\Http\Request {#43 ▼
#json: Symfony\Component\HttpFoundation\ParameterBag {#35 ▶}
#convertedFiles: []
#userResolver: Closure($guard = null) {#1241 ▼
class: "Illuminate\Auth\AuthServiceProvider"
this: Illuminate\Auth\AuthServiceProvider {#22 …}
use: {▶}
file: "C:\xampp\htdocs\attendance_online\vendor\laravel\framework\src\Illuminate\Auth\AuthServiceProvider.php"
line: "105 to 107"
}
#routeResolver: Closure() {#1246 ▼
class: "Illuminate\Routing\Router"
this: Illuminate\Routing\Router {#26 …}
use: {▶}
file: "C:\xampp\htdocs\attendance_online\vendor\laravel\framework\src\Illuminate\Routing\Router.php"
line: "655 to 657"
}
+attributes: Symfony\Component\HttpFoundation\ParameterBag {#45 ▼
#parameters: []
}
+request: Symfony\Component\HttpFoundation\ParameterBag {#35 ▼
#parameters: []
}
+query: Symfony\Component\HttpFoundation\InputBag {#51 ▼
#parameters: []
}
+server: Symfony\Component\HttpFoundation\ServerBag {#47 ▼
#parameters: array:23 [▶]
}
+files: Symfony\Component\HttpFoundation\FileBag {#48 ▼
#parameters: []
}
+cookies: Symfony\Component\HttpFoundation\InputBag {#46 ▼
#parameters: []
}
+headers: Symfony\Component\HttpFoundation\HeaderBag {#49 ▼
#headers: array:6 [▼
"accept-encoding" => array:1 [▼
0 => "gzip,deflate"
]
"content-type" => array:1 [▼
0 => "application/json"
]
"content-length" => array:1 [▼
0 => "231"
]
"host" => array:1 [▼
0 => "localhost:8000"
]
"connection" => array:1 [▼
0 => "Keep-Alive"
]
"user-agent" => array:1 [▼
0 => "Apache-HttpClient/4.5.5 (Java/12.0.1)"
]
]
#cacheControl: []
}
#content: """
{\n
\t"id_employee" = "4",\n
\t"status" = "1",\n
\t"latitude" = "3.4141414",\n
\t"longitude" = "98.444444",\n
\t"timestamp" = "2020-10-11 09:58:00.0000000",\n
\t"approval" = "1",\n
\t"message" = "this is message",\n
\t"picture_reference" = "this is pic"\n
}
"""
#languages: null
#charsets: null
#encodings: null
#acceptableContentTypes: null
#pathInfo: "/api/Attendance"
#requestUri: "/api/Attendance"
#baseUrl: ""
#basePath: null
#method: "POST"
#format: null
#session: null
#locale: null
#defaultLocale: "en"
-preferredFormat: null
-isHostValid: true
-isForwardedValid: true
-isSafeContentPreferred: null
basePath: ""
format: "html"
}
}
当我 return $request->getcontent() 结果是
{ "id_employee" = "4", "status" = "1", "latitude" = "3.4141414", "longitude" = "98.444444", "timestamp" = "2020-10-11 09:58:00.0000000", "approval" = "1", "message" = "this is message", "picture_reference" = "this is pic" }
但是当我 return $request->all() 是说
<Empty JSON content>
每当我使用 $request->id_employee 或任何其他数据时,它总是 return 为空。当我将参数放在 URL 上时,如“?id_employee=1”,$request->id_employee 将 return 1. 我不想将数据放在URL,那么如何获取 $request 上的所有内容?
编辑: 我已经试过了
$id_employee = $request->id_employee;
$id_employee = $request->input("id_employee")
$id_employee = $request->input("content.id_employee")
并且已经确认 $request->isjson() return 1
你有什么版本的Laravel?您应该能够像这样解码 JSON 内容:
$content = $request->getContent()
$data = json_decode($content, true)
dd(data)
不过请检查 JSON 是否有效 - 您在原始问题中发布的示例 =
通常应该放在 :
的位置! JSON 需要有效,如果请求具有正确的 Content-Type header,那么您对 request->input("employee_id")
的调用应该有效。这并没有让我想知道 JSON 是否是 well-formatted 所以一定要检查一下。