Back

BasicNetwork.performRequest: Unexpected response code 500


Oshaba Samson posted 6 months ago

I am developing an android application and using laravel and mysql as the backend. when I post data from postman to mysql through laravel it goes through. But for android it displays the above error if the create function is there, returns the json data below if create function is not there just like postman. Here is my code Android code


              RequestQueue requestQueue = Volley.newRequestQueue(SignUpActivity.this);
                try {
                    JSONObject jsonBody = new JSONObject();
                    jsonBody.put("username","amz");
                    jsonBody.put("email","[email protected]");
                    jsonBody.put("password","fashion");
                    jsonBody.put("vCode","12345");

                    JsonObjectRequest request_json = new JsonObjectRequest(url,jsonBody,
                            new Response.Listener<JSONObject>() {
                                @Override
                                public void onResponse(JSONObject response) {
                                  Log.i("info", response.toString());
                                }
                            }, new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            VolleyLog.e("Error: ", error.getMessage());
                        }
                    });
                    requestQueue.add(request_json);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

laravel code


 public function signUp(Request $request){
        $success = "Successful";
        $validator = Validator::make($request->all(), [
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|min:5'
        ]);

        if ($validator->fails()){
            return response()->json(['error'=>$validator->errors()], 401);
        };

        try{

            User::create([
                'email' => $request->get('email'),
                'username' => $request->get('username'),
                'password' => Hash::make($request->get('password')),
                'vCode' => $request->get('vCode')
            ]);
        }catch (Exception $exception){
            return response()->json(['error', $exception->getMessage()], 300);
        }

         return $request->all();
      //  return response()->json(['success'=> $success], $this->successStatus);
    }

Output from postman and android

{
    "email": "[email protected]",
    "password": "jawe55rwwmes",
    "username": "john43eeewson",
    "vCode": "983wer54ew23"
}

Thanks, all answers will be appreciated

slillo replied 6 months ago

Maybe, via postman you're not passing request body as json. Try this in your controller:

$parameters = request()->json();
//access to parameters using $parameters->get("email")

Sign in to participate in this thread!



We'd like to thank these amazing companies for supporting us