我正在调用laravel控制器中的一个函数。我使用了laravel的自定义响应来返回数据。但是一些其他的数据被添加到我的响应中,尽管我没有添加它。
return Response::make(json_encode(array(
'error' => false,
'message' => 'Redaction done successfully.',
'is_redacted' => 0)), 200,
array(
'Content-Type' => 'application/json'
)
);
字符串
上面是我添加的返回我的响应的代码。但是我收到的响应是:
> * Found bundle for host redaction-stage-tcm.tylerhost.net: 0x55998c67ee40
* Re-using existing connection! (#0) with host redaction-stage-tcm.tylerhost.net
* Connected to redaction-stage-tcm.tylerhost.net (208.64.239.110) port 443 (#0)
> POST /ocr/OCRWeb/v2/Documents('mfr_8')/OCRWeb.Scan HTTP/1.1
Host: redaction-stage-tcm.tylerhost.net
User-Agent: GuzzleHttp/6.3.3 curl/7.43.0 PHP/5.6.11-1ubuntu3.4
OData-Version: 4.01
OData-MaxVersion: 4.01
Content-Type: application/json
Content-Length: 2
upload completely sent off: 2 out of 2 bytes
< HTTP/1.1 200
< Cache-Control: no-transform
< Cache-Control: no-cache
< Cache-Control: no-store
< Cache-Control: must-revalidate
< Cache-Control: max-age=0
< Expires: Thu, 01 Jan 1970 00:00:00 GMT
< Vary: *
< Pragma: no-cache
< X-Clacks-Overhead: GNU Terry Pratchett
< OData-Version: 4.01
< Preference-Applied:
< Content-Type: application/json; odata.metadata=minimal
< Content-Length: 163
< Date: Wed, 13 Feb 2019 10:44:40 GMT
<
* Connection #0 to host redaction-stage-tcm.tylerhost.net left intact
{"error":false,"message":"Redaction done successfully.","is_redacted":0}
型
我在函数中调用了一些REST API。这个数据与此相关,但为什么它会附加到我的自定义响应中??请帮助我。
3条答案
按热度按时间ecbunoof1#
试试这个
字符串
35g0bw712#
创建一个名为
AddToRequest
的中间件,并将其注册到Kernel.php中。AddToRequest.php
字符串
Kernel.php
型
在
AddToRequest
中间件的handle()
方法中,我设置了两个变量并将其添加到请求中型
然后,在我的控制器中使用
inject
中间件型
在控制器的
index()
方法中,我注入useIlluminate\Http\Request
对象,并从请求对象中提取我在中间件中设置的变量,如下所示:型
eivgtgni3#
为了防止你的情况和我的情况类似,我试图在Laravel的log中使用print_r从API控制器中调用的函数中记录一个对象。我使用了print_r,但没有使用“true”开关,结果不是将其作为字符串返回,而是将其发送到标准输出,从而将其置于控制器的正常响应之前。将log命令更改为:
字符串
帮我解决了问题
print_r(不带 true 参数)、var_dump和var_export(没有 true 参数),本质上是试图“在屏幕上打印”您提供的任何变量作为参数,并且由于API调用不直接将其响应返回到屏幕,因此这些函数的输出被预先添加到API通常返回的响应中。或者将它们与true参数(对于print_r和var_export)一起使用,将它们的输出作为字符串返回,而不是将其发送到标准输出,这将允许API仅返回其预期的响应。