access laravel app from android app with csrf token access laravel app from android app with csrf token laravel laravel

access laravel app from android app with csrf token


If you don't want to disable CSRF tokens, then you will need to retrieve the CSRF in one request, then pass the retrieved token along with your POST request.

// Create a new HttpClient and Post HeaderHttpClient httpclient = new DefaultHttpClient();// Get the CSRF tokenhttpClient.execute(new HttpGet("http://www.yoursite.com/"));CookieStore cookieStore = httpClient.getCookieStore();List <Cookie> cookies =  cookieStore.getCookies();for (Cookie cookie: cookies) {    if (cookie.getName().equals("XSRF-TOKEN")) {        CSRFTOKEN = cookie.getValue();    }}// Access POST route using CSRFTOKENHttpPost httppost = new HttpPost("http://www.yoursite.com/your-post-route");try {    // Add your data    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);    nameValuePairs.add(new BasicNameValuePair("_token", CSRFTOKEN));    nameValuePairs.add(new BasicNameValuePair("stringdata", "Hello!"));    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));    // Execute HTTP Post Request    HttpResponse response = httpclient.execute(httppost);} catch (ClientProtocolException e) {    // TODO Auto-generated catch block} catch (IOException e) {    // TODO Auto-generated catch block}


I tried

...nameValuePairs.add(new BasicNameValuePair("_token", CSRFTOKEN));...

But it doesn't work

If you can try

request.addHeader("X-CSRF-Token", token);

it works for me