Ramin
Ramin

Reputation: 57

send json to Laravel using postman and android

i am trying to send json using postman to Lavavel but i facing this error.

enter image description here this is my json code:

{
    "email":"[email protected]",
    "password":"testtest"
}

and this is Laravel codes :

Route::get('/r','test@store');

and

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use Log;
class test extends Controller
{
    public function store(Request $request)
    {
        $email = $request->input('email');
        $password = $request->input('password');

        Log::info('test');
        Log::info($email);
        Log::info($password);

        DB::table('login')->insert([
            ['email' =>  $email],
            ['password' =>  $password]
        ]);
    }
}

also i trying using android for send data using volley and so checked Laravel logs :

Column 'email' cannot be null (this is Laravel logs)

and on android Logs:

E/Volley: [299] BasicNetwork.performRequest: Unexpected response code 500 for http://192.168.1.4:8000/r D/error: com.android.volley.ServerErro

my android code is :

public class ApiService {


    private final Context context;

    public ApiService(Context context){
        this.context=context;
    }

            public void loginUser(String email, String password, final OnLoginResponse onLoginResponse){
                JSONObject requestJsonObject=new JSONObject();
                try {
                    requestJsonObject.put("email",email);
                    requestJsonObject.put("password",password);


                    JsonObjectRequest request=new JsonObjectRequest(Request.Method.GET, "http://192.168.1.4:8000/r",requestJsonObject , new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject response) {
                            Log.d("response",response.toString());
                        }

                    }, new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            Log.d("error",error.toString());
                        }
                    }) {
                        @Override
                        public Map getHeaders() throws AuthFailureError {
                            HashMap headers = new HashMap();
                            headers.put("Content-Type", "application/json");
                            return headers;
                        }
                    };
                    request.setRetryPolicy(new DefaultRetryPolicy(18000,DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
                    Volley.newRequestQueue(context).add(request);
                } catch (JSONException e) {
                    Log.e(TAG, "loginUser: "+e.toString());
                }
            }

    public interface OnLoginResponse{
        void onResponse(boolean success);
    }
}

Upvotes: 0

Views: 4263

Answers (2)

Martin Lloyd Jose
Martin Lloyd Jose

Reputation: 470

I hope this helps people trying to search on how to send JSON data to laravel not only specific to android applications but to all. The goal of this solution is to identify whether you can send a JSON data to laravel or not.

First of all you have to download postman from https://www.getpostman.com/ to test if your API is really working or not.

Create a post request using postman. Be sure that you follow the example data below enter image description here

Be sure that you set your Routes that would associate to the controllerenter image description here

This is the controller part that will show the JSON data you sent if it was successfully accepted or not. enter image description here

And also, if ever you are trying to send POST data to laravel, by default they provided a CSRF Token which is applicable for the forms if you are going to use the MVC of laravel. For the meantime, we are going to take this down and comment it out. Just go to app/http/kernel.php enter image description here

and now you'll get the following result from the code earlier

$json = json_decode($request['json']);
echo $json->{'email'};
echo "\n";
echo $json->{'password'};

enter image description here We tested that we were able to send data to laravel. I hope this truly helps.

Upvotes: 2

Marco Capo
Marco Capo

Reputation: 256

Wen you want to send data, you will want to use POST or PUT method on your postman, specially if you are sending a body, that means that you are sending data. Get method is used to retrieve data from a service. Take a look into CRUD functions for more information. Your postman should look something like this

Last in your android code try to change this line

JsonObjectRequest request=new JsonObjectRequest(Request.Method.GET, "http://192.168.1.4:8000/r",requestJsonObject , new Response.Listener<JSONObject>() {

to use Request.Method.POST

Upvotes: 0

Related Questions