Reputation: 146
I am trying to create a desktop app using c# that will manipulate my Laravel website's database(MySQL). So I am creating API routes to act as a middleman. I actually solved my issue by changing the code but did not understand why it was not working earlier. I am new to both Laravel & c# and coding some stuff just to learn some things.
WebClient client = new WebClient();
string postUrl = "http://mywebsite.com/api/v1/handshake";
var reqparm = new System.Collections.Specialized.NameValueCollection();
reqparm.Add("param1", "<any> kinds & of = ? strings");
reqparm.Add("param2", "testing parameter two");
byte[] response= client.UploadValues(postUrl, reqparm);
string result = System.Text.Encoding.UTF8.GetString(response);
MessageBox.Show(result);
This is my route in Laravel (Outside of middleware)
Route::prefix('v1')->group(function(){
Route::post('handshake','Api\SomeController@handshake');
});
The problem that I'm curious about is when my controller was like this :
public function handshake(Request $request)
{
return json_encode($request);
}
Respone was : {"attributes":{},"request":{},"query":{},"server":{},"files":{},"cookies":{},"headers":{}}
When I change the handshake method to this :
public function handshake(Request $request)
{
return json_encode($request->param2);
}
The response was: "testing parameter two"
So I wonder why the response was empty when I returned json_encode($response).
Is Illuminate Request a different type of object that causes this, or is it something else I am missing? It will be very helpful if you explain the reason why it's not worked
Upvotes: 1
Views: 705
Reputation: 29306
json_encode
ing $request
(or request()
) doesn't display -
(private) or #
(protected) properties, only +
(public). See this php artisan tinker
example:
>>> request()
=> Illuminate\Http\Request {#70
+attributes: Symfony\Component\HttpFoundation\ParameterBag {#75},
+request: Symfony\Component\HttpFoundation\ParameterBag {#72},
+query: Symfony\Component\HttpFoundation\ParameterBag {#77},
+server: Symfony\Component\HttpFoundation\ServerBag {#79},
+files: Symfony\Component\HttpFoundation\FileBag {#73},
+cookies: Symfony\Component\HttpFoundation\ParameterBag {#76},
+headers: Symfony\Component\HttpFoundation\HeaderBag {#80},
}
As you can see, in Terminal access, it's only showing +
(public) properties. Compare with dd(request())
(which shows all properties, regardless of access level):
>>> dd(request())
Illuminate\Http\Request^ {#70
#json: null
#convertedFiles: null
#userResolver: null
#routeResolver: null
+attributes: Symfony\Component\HttpFoundation\ParameterBag^ {#75
#parameters: []
}
+request: Symfony\Component\HttpFoundation\ParameterBag^ {#72
#parameters: []
}
+query: Symfony\Component\HttpFoundation\ParameterBag^ {#77
#parameters: []
}
+server: Symfony\Component\HttpFoundation\ServerBag^ {#79
#parameters: array:132 [...]
}
+files: Symfony\Component\HttpFoundation\FileBag^ {#73
#parameters: []
}
+cookies: Symfony\Component\HttpFoundation\ParameterBag^ {#76
#parameters: []
}
+headers: Symfony\Component\HttpFoundation\HeaderBag^ {#80
#headers: array:5 [...]
}
#content: null
#languages: null
#charsets: null
#encodings: null
#acceptableContentTypes: null
#pathInfo: null
#requestUri: null
#baseUrl: null
#basePath: null
#method: null
#format: null
#session: null
#locale: null
#defaultLocale: "en"
-preferredFormat: null
-isHostValid: true
-isForwardedValid: true
pathInfo: "/"
requestUri: "/"
baseUrl: ""
basePath: ""
method: "GET"
format: "html"
}
So, when you run json_encode($request)
, it converts that instance of Illuminate\Http\Request
to json
, but since most properties are #
(protected) or -
(private), a lot is stripped out. And even then, for the displayed properties, the only nested property is #parameters
, which is protected, and thus stripped out.
So, json_encode(request())
produces the correct JSON
:
{"attributes":{},"request":{},"query":{},"server":{},"files":{},"cookies":{},"headers":{}}
Upvotes: 2