I can't to consume my Laravel 4 API with an android app. But I'm not able to do that
This is my routes.php
Route::group(['prefix' => 'api'], function(){
Route::group(['prefix' => 'v1', 'namespace' => 'api\v1'], function(){
Route::resource('person', 'PersonsRestController', array('only' => array('store', 'index')));
});
});
And this is my PersonsRestController.php
namespace api\v1;
class PersonsRestController extends \BaseController {
// The method is GET
public function index() {
dd("Index: ".$_SERVER['REQUEST_METHOD']);
}
// The method is POST
public function store() {
dd("Store: ".$_SERVER['REQUEST_METHOD']);
}
}
I tried to make an STORE operation (POST http method), But I always get the message of INDEX
I use this code for test my API rest:
public static void main(String[] args) throws Exception {
URL url = new URL("http://foo.com/api/v1/person/");
Map<String, Object> params = new LinkedHashMap<>();
params.put("name", "Freddie the Fish");
params.put("email", "fishie@seamail.example.com");
params.put("reply_to_thread", 10394);
params.put("message", "Shark attacks in Botany Bay have gotten out of control. We need more defensive dolphins to protect the schools here, but Mayor Porpoise is too busy stuffing his snout with lobsters. He's so shellfish.");
StringBuilder postData = new StringBuilder();
for (Map.Entry<String, Object> param : params.entrySet()) {
if (postData.length() != 0) {
postData.append('&');
}
postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
postData.append('=');
postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
}
byte[] postDataBytes = postData.toString().getBytes("UTF-8");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
conn.setDoOutput(true);
conn.getOutputStream().write(postDataBytes);
Reader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
for (int c = in.read(); c != -1; c = in.read()) {
System.out.print((char) c);
}
}
Source of code: https://stackoverflow.com/a/21657510/4359029
I hope someone can help me with my problem...
EDIT:
I also tried with this directly:
Route::post('test', 'MyTestController@testMethod');
but i get the next error: Server returned HTTP response code: 405 for URL: http://foo.bar/api/laravel
So laravel doesnt found my post route