1

i have problems with my Webservice i created in php with the Laravel Framework.

I have the following URL to call:

http://localhost:100/cust/server.php/InitialSync/{"IdCard": "lxpIu1bD4UX4W2h5EM+i6VEQUZk+i\/SJF1DU6179HBejWkOBENSflnTSN\/8N14OGTqh6fH\/6kNrjJCilCMIrVtrlUAyQ5y8zZXVy5K3XwMOGmlHghAe80Q=="}

So you see that i send a Json Object with an crypted IdCard to the Server. My route looks like that:

Route::get('InitialSync/{idCard}, 'SyncController@InitialSync'};

So Problem is that this won´t work. I think the Problems are the / in the JsonObject.

Does anyone of you know how i can solve this problem.

The Result from Laravel is:

Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException

I think he tries to find the Route but becouse of the / in the Json Object i get this error.

Laurence
  • 58,936
  • 21
  • 171
  • 212
Alex
  • 600
  • 1
  • 7
  • 20

2 Answers2

4

Unfortunately Laravel can't undestand your browser will have trouble to process a JSON sent via GET, even if you encode and stringify it via Javascript:

encodeURIComponent(
  JSON.stringify(
    {"IdCard": "lxpIu1bD4UX4W2h5EM+i6VEQUZk+i\/SJF1DU6179HBejWkOBENSflnTSN\/8N14OGTqh6fH\/6kNrjJCilCMIrVtrlUAyQ5y8zZXVy5K3XwMOGmlHghAe80Q=="}
  )
)

Wich generates this string:

"%7B%22IdCard%22%3A%22lxpIu1bD4UX4W2h5EM%2Bi6VEQUZk%2Bi%2FSJF1DU6179HBejWkOBENSflnTSN%2F8N14OGTqh6fH%2F6kNrjJCilCMIrVtrlUAyQ5y8zZXVy5K3XwMOGmlHghAe80Q%3D%3D%22%7D"

Laravel will still have matters to recognize a route in that URL. This will not work.

The source of the problem is the escaped characters \/ present in the string. So you have some options:

1) Send it via POST

2) Base64 encode the IdCard and decode is back in Laravel.

3) Replace those characters by something else and revert it in Laravel.

4) Fill a bug in Laravel's Github repo and wait for them to fix it.

Antonio Carlos Ribeiro
  • 86,191
  • 22
  • 213
  • 204
  • okay, i tried your solution now and i have the same problem i get error 404. My Problem is that i send a crypted string (AES) and my Laravel Route has to accept that. – Alex Feb 18 '14 at 17:29
  • My solution? I gave no solution, I gave you 3 options and none of those is a solution. After the code I shown I said `Laravel will still have matters to recognize a route in that URL.`, which means that not even this will make it work. – Antonio Carlos Ribeiro Feb 18 '14 at 18:03
  • okay... so that means, i have no change to handle this with a Get Request. So the only Solution is to make a Post and not a Get. Do you agree with this? – Alex Feb 18 '14 at 18:16
  • Yeah, if you can make a POST, it will succeed, but sometimes we just can't. Just edited to add this to my answer. – Antonio Carlos Ribeiro Feb 18 '14 at 18:28
0

You could do something like this:

1) on your router:

Route::get('/readjson/{json}', 'MyController@readJson');

2) on your controller:

class MyController extends BaseController {

    public function readJson() {
        dd(request()->segments());
    }
}

That should print all the segments of the requested URI, including the param sent as a JSON string

3) Then just replace segments() with segment(n) to get the exact one. Should be number 2 in this case or 3 if you're using api routes (routes/api.php). From there you can use json_decode or anything else to decode the JSON string.

Zedzdead
  • 356
  • 3
  • 18