Api 检索JSON内容
我是拉威尔的初学者。我在创建PostAPI时遇到问题。 我想使用从其他应用程序的json POST检索的数据。但是它总是返回null,所以我对请求做了一些调查 当我返回dd($request)时,结果是Api 检索JSON内容,api,laravel-7,Api,Laravel 7,我是拉威尔的初学者。我在创建PostAPI时遇到问题。 我想使用从其他应用程序的json POST检索的数据。但是它总是返回null,所以我对请求做了一些调查 当我返回dd($request)时,结果是 Illuminate\Http\Request {#43 ▼ #json: Symfony\Component\HttpFoundation\ParameterBag {#35 ▶} #convertedFiles: [] #userResolver: Closure($guard
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"
}
}
{ "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" }
当我返回$request->getcontent()时,结果是
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"
}
}
{ "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" }
但是当我返回$request->all()时
并且已经确认$request->isjson()返回1您有什么版本的Laravel?您应该能够解码JSON内容,如下所示:
$content=$request->getContent()
$data=json_decode($content,true)
dd(数据)
不过,请检查JSON是否有效-您在原始问题中发布的示例有
=
,其中:
通常应该放在哪里!JSON必须有效,如果请求具有正确的内容类型头,那么对request->input(“employee_id”)
的调用应该可以工作。这并没有让我怀疑JSON是否格式正确,所以一定要检查一下。这行不是已经显示请求是有效的JSON post请求了吗<代码>+标题:Symfony\Component\HttpFoundation\HeaderBag{49▼ #标题:数组:6[▼ “接受编码”=>数组:1[▼ 0=>“gzip,deflate”]“内容类型”=>数组:1[▼ 0=>“application/json”]我的json似乎格式不好,因为我发现了小的错误类型。我已经决定将数据放在url(参数)上,但是,再次检查后,在关闭括号之前,似乎有1个野生逗号,我假设json不可读。