'Laravel returns request content with response
I have an API on Laravel, I did project some time ago, but now when I returned to it I have a problem. When I do POST request on API, I get response with request. For example endpoint: https://ttr-api.somniumgame.com/api/user_info
I send for testing (can be any data) json data:
{
"auth_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhY2NvdW50X2lkIjoiVGVzdCIsIm5pY2tuYW1lIjoiTG9vc2VyIiwiZGV2aWNlIjoiaW9zIiwiaXNzIjoiU29tbml1bUdhbWUiLCJhdWQiOiJ0dHItYXBpLnNvbW5pdW1nYW1lLmNvbSIsImlhdCI6MTY1MDE0MDE1MywiZXhwIjoxNjUwMTQwMjEzfQ.9nPgzn4wZEDGaN4rqvyW5mY5fZspt8HfUpm6lgzFin0"
}
I get response:
HTTP/1.1 200 OK
Server: nginx/1.20.2
Date: Sun, 17 Apr 2022 07:22:19 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
X-Powered-By: PHP/8.1.3
{
"auth_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhY2NvdW50X2lkIjoiVGVzdCIsIm5pY2tuYW1lIjoiTG9vc2VyIiwiZGV2aWNlIjoiaW9zIiwiaXNzIjoiU29tbml1bUdhbWUiLCJhdWQiOiJ0dHItYXBpLnNvbW5pdW1nYW1lLmNvbSIsImlhdCI6MTY1MDE0MDE1MywiZXhwIjoxNjUwMTQwMjEzfQ.9nPgzn4wZEDGaN4rqvyW5mY5fZspt8HfUpm6lgzFin0"
}{"error":"Expired token"}
But in my code controller do:
if (isset($data['error'])) return response()->json(['error' => $data['error']], 400);
Why I get 200 code instead of 400? Why I have request body inside response?
I have in Kernel:
<?php
namespace App\Http;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
class Kernel extends HttpKernel
{
/**
* The application's global HTTP middleware stack.
*
* These middleware are run during every request to your application.
*
* @var array<int, class-string|string>
*/
protected $middleware = [
// \App\Http\Middleware\TrustHosts::class,
// \App\Http\Middleware\TrustProxies::class,
\Illuminate\Http\Middleware\HandleCors::class,
\App\Http\Middleware\PreventRequestsDuringMaintenance::class,
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
\App\Http\Middleware\TrimStrings::class,
\App\Http\Middleware\LogAfterRequest::class,
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
];
/**
* The application's route middleware groups.
*
* @var array<string, array<int, class-string|string>>
*/
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
// \Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
'api' => [
// \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
'throttle:api',
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
];
/**
* The application's route middleware.
*
* These middleware may be assigned to groups or used individually.
*
* @var array<string, class-string|string>
*/
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'password.confirm' => \Illuminate\Auth\Middleware\RequirePassword::class,
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
'jwt.verify' => \App\Http\Middleware\JwtMiddleware::class,
];
}
I think it can be: \App\Http\Middleware\LogAfterRequest::class, But when I comment this line nothing changes. Code of this middleware:
<?php
namespace App\Http\Middleware;
use Illuminate\Support\Facades\Log;
class LogAfterRequest {
public function handle($request, \Closure $next)
{
return $next($request);
}
public function terminate($request, $response)
{
Log::info('app.requests', ['request' => $request->all(), 'response' => $response]);
}
}
Where can be problem? I have no ideas...
P.S. When we use post request WITHOUT any body we got correct status code: Can be tested here online now: https://reqbin.com
P.S.S. I use Nginx, maybe here can be problem but I nothing found what difference with other project API and it work fine...
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
# configuration file /etc/nginx/nginx.conf:
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
proxy_read_timeout 1200;
proxy_connect_timeout 240;
client_max_body_size 0;
map $http_upgrade $connection_upgrade {
default upgrade;
'' '';
}
include /etc/nginx/conf.d/*.conf;
}
# configuration file /etc/nginx/mime.types:
types {
text/html html htm shtml;
text/css css;
text/xml xml;
image/gif gif;
image/jpeg jpeg jpg;
application/javascript js;
application/atom+xml atom;
application/rss+xml rss;
text/mathml mml;
text/plain txt;
text/vnd.sun.j2me.app-descriptor jad;
text/vnd.wap.wml wml;
text/x-component htc;
image/png png;
image/svg+xml svg svgz;
image/tiff tif tiff;
image/vnd.wap.wbmp wbmp;
image/webp webp;
image/x-icon ico;
image/x-jng jng;
image/x-ms-bmp bmp;
font/woff woff;
font/woff2 woff2;
application/java-archive jar war ear;
application/json json;
application/mac-binhex40 hqx;
application/msword doc;
application/pdf pdf;
application/postscript ps eps ai;
application/rtf rtf;
application/vnd.apple.mpegurl m3u8;
application/vnd.google-earth.kml+xml kml;
application/vnd.google-earth.kmz kmz;
application/vnd.ms-excel xls;
application/vnd.ms-fontobject eot;
application/vnd.ms-powerpoint ppt;
application/vnd.oasis.opendocument.graphics odg;
application/vnd.oasis.opendocument.presentation odp;
application/vnd.oasis.opendocument.spreadsheet ods;
application/vnd.oasis.opendocument.text odt;
application/vnd.openxmlformats-officedocument.presentationml.presentation
pptx;
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
xlsx;
application/vnd.openxmlformats-officedocument.wordprocessingml.document
docx;
application/vnd.wap.wmlc wmlc;
application/x-7z-compressed 7z;
application/x-cocoa cco;
application/x-java-archive-diff jardiff;
application/x-java-jnlp-file jnlp;
application/x-makeself run;
application/x-perl pl pm;
application/x-pilot prc pdb;
application/x-rar-compressed rar;
application/x-redhat-package-manager rpm;
application/x-sea sea;
application/x-shockwave-flash swf;
application/x-stuffit sit;
application/x-tcl tcl tk;
application/x-x509-ca-cert der pem crt;
application/x-xpinstall xpi;
application/xhtml+xml xhtml;
application/xspf+xml xspf;
application/zip zip;
application/octet-stream bin exe dll;
application/octet-stream deb;
application/octet-stream dmg;
application/octet-stream iso img;
application/octet-stream msi msp msm;
audio/midi mid midi kar;
audio/mpeg mp3;
audio/ogg ogg;
audio/x-m4a m4a;
audio/x-realaudio ra;
video/3gpp 3gpp 3gp;
video/mp2t ts;
video/mp4 mp4;
video/mpeg mpeg mpg;
video/quicktime mov;
video/webm webm;
video/x-flv flv;
video/x-m4v m4v;
video/x-mng mng;
video/x-ms-asf asx asf;
video/x-ms-wmv wmv;
video/x-msvideo avi;
}
# configuration file /etc/nginx/conf.d/office.somniumgame.com.conf:
server {
listen 80;
server_name office.somniumgame.com www.office.somniumgame.com;
return 301 https://office.somniumgame.com$request_uri;
large_client_header_buffers 4 16k;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name office.somniumgame.com www.office.somniumgame.com;
root /var/www/html/public;
ssl_certificate /etc/letsencrypt_office/live/office.somniumgame.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt_office/live/office.somniumgame.com/privkey.pem;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 10m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
large_client_header_buffers 4 16k;
index index.php;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
location ~ \.php$ {
fastcgi_pass teamcity-linux-agent:9001;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
# configuration file /etc/nginx/fastcgi_params:
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REQUEST_SCHEME $scheme;
fastcgi_param HTTPS $https if_not_empty;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;
# configuration file /etc/nginx/conf.d/somniumgame.com.conf:
server {
listen 80;
server_name somniumgame.com www.somniumgame.com;
return 301 https://somniumgame.com$request_uri;
large_client_header_buffers 4 16k;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name somniumgame.com www.somniumgame.com;
root /usr/share/nginx/html;
ssl_certificate /etc/letsencrypt_root/live/somniumgame.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt_root/live/somniumgame.com/privkey.pem;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 10m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
large_client_header_buffers 4 16k;
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
location / {
proxy_pass http://teamcity-linux-agent:3000;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
client_max_body_size 100m;
}
}
#}
# configuration file /etc/nginx/conf.d/ttr-api.somniumgame.com.conf:
server {
listen 80;
server_name ttr-api.somniumgame.com www.ttr-api.somniumgame.com;
return 301 https://ttr-api.somniumgame.com$request_uri;
large_client_header_buffers 4 16k;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name ttr-api.somniumgame.com www.ttr-api.somniumgame.com;
root /var/www/html/public;
ssl_certificate /etc/letsencrypt_ttr/live/ttr-api.somniumgame.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt_ttr/live/ttr-api.somniumgame.com/privkey.pem;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 10m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
large_client_header_buffers 4 16k;
location / {
try_files $uri /index.php?$args;
}
location ~ \.php$ {
fastcgi_pass teamcity-linux-agent:9000;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
P.S.S.S. I added this route and in this case we have same problem, I think problem not inside controller
Route::post('test', function () {
return response()->json('SOME TEXT');
});
HTTP/1.1 200 OK
Server: nginx/1.20.2
Date: Sun, 17 Apr 2022 09:44:03 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
X-Powered-By: PHP/8.1.3
{
"auth_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhY2NvdW50X2lkIjoiVGVzdCIsIm5pY2tuYW1lIjoiTG9vc2VyIiwiZGV2aWNlIjoiaW9zIiwiaXNzIjoiU29tbml1bUdhbWUiLCJhdWQiOiJ0dHItYXBpLnNvbW5pdW1nYW1lLmNvbSIsImlhdCI6MTY1MDE0MDE1MywiZXhwIjoxNjUwMTQwMjEzfQ.9nPgzn4wZEDGaN4rqvyW5mY5fZspt8HfUpm6lgzFin0"
}"SOME TEXT"
Solution 1:[1]
kinsing malware - reason of this problem... Needs to delete line in crontab and delete everything in /tmp folder
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | FunnyCOFE |