下記リンクサイト様を参考に、Android app から Laravel appへPOST送信するコードを作成しようと試みていますが実現できません。
https://stackoverflow.com/questions/31627477/access-laravel-app-from-android-app-with-csrf-token
上記の参照コードは、非推奨となっているメソッドが幾つかありましたので、色々調べながら下記コードを考えたのですが、上手くいきません。Internal Server Errorになります。作成しているコードは、接続したいLaravel app のXSRF-TOKENを取得して、post送信モードにする、という感じのコードです。
Android app から Laravel appに、Post送信するのには、どうしたらいいのでしょうか?
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = findViewById(R.id.textView); new Thread(new Runnable() { @Override public void run() { String site = "http://192.168.xxx.xxx:8000"; try { HttpURLConnection getConnection = (HttpURLConnection) new URL(site).openConnection(); Map<String,List<String>> header = getConnection.getHeaderFields(); List<String> cookies = header.get("set-cookie"); String cookie = cookies.get(0); String token = HttpCookie.parse(cookie).get(0).getValue(); HttpURLConnection postConnection = (HttpURLConnection) new URL(site).openConnection(); postConnection.setRequestMethod("POST"); postConnection.setDoOutput(true); postConnection.setRequestProperty("X-XSRF-TOKEN",token); postConnection.connect(); getConnection.disconnect(); postConnection.disconnect(); } catch (IOException e) { e.printStackTrace(); } } }).start(); }
■ Laravel側のコードです
Route
1Route::get ('/','Laravel01Controller@getIndex'); 2Route::post ('/','Laravel01Controller@postIndex');
Controller
1class Laravel01Controller extends Controller 2{ 3 public function getIndex(Request $request){ 4 return view('hello'); 5 } 6 7 public function postIndex(Request $request){ 8 return view('hello'); 9} }
hello
1hello
■ エラー情報です
通信方法を'get'で指定すると、リクエストコードは200が返ってきます。
[2019-10-05 14:23:46] local.ERROR: The payload is invalid. {"exception":"[object] (Illuminate\Contracts\Encryption\DecryptException(code: 0): The payload is invalid. at D:\privatePro\laravel\larave01\vendor\laravel\framework\src\Illuminate\Encryption\Encrypter.php:195) [stacktrace] #0 D:\privatePro\laravel\larave01\vendor\laravel\framework\src\Illuminate\Encryption\Encrypter.php(136): Illuminate\Encryption\Encrypter->getJsonPayload(NULL) #1 D:\privatePro\laravel\larave01\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\VerifyCsrfToken.php(154): Illuminate\Encryption\Encrypter->decrypt('eyJpdiI6ImhBeW4...', false) #2 D:\privatePro\laravel\larave01\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\VerifyCsrfToken.php(136): Illuminate\Foundation\Http\Middleware\VerifyCsrfToken->getTokenFromRequest(Object(Illuminate\Http\Request)) #3 D:\privatePro\laravel\larave01\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\VerifyCsrfToken.php(74): Illuminate\Foundation\Http\Middleware\VerifyCsrfToken->tokensMatch(Object(Illuminate\Http\Request)) #4 D:\privatePro\laravel\larave01\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(171): Illuminate\Foundation\Http\Middleware\VerifyCsrfToken->handle(Object(Illuminate\Http\Request), Object(Closure)) #5 D:\privatePro\laravel\larave01\vendor\laravel\framework\src\Illuminate\View\Middleware\ShareErrorsFromSession.php(49): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request)) #6 D:\privatePro\laravel\larave01\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(171): Illuminate\View\Middleware\ShareErrorsFromSession->handle(Object(Illuminate\Http\Request), Object(Closure)) #7 D:\privatePro\laravel\larave01\vendor\laravel\framework\src\Illuminate\Session\Middleware\StartSession.php(56): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request)) #8 D:\privatePro\laravel\larave01\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(171): Illuminate\Session\Middleware\StartSession->handle(Object(Illuminate\Http\Request), Object(Closure)) #9 D:\privatePro\laravel\larave01\vendor\laravel\framework\src\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse.php(37): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request)) #10 D:\privatePro\laravel\larave01\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(171): Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse->handle(Object(Illuminate\Http\Request), Object(Closure)) #11 D:\privatePro\laravel\larave01\vendor\laravel\framework\src\Illuminate\Cookie\Middleware\EncryptCookies.php(66): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request)) #12 D:\privatePro\laravel\larave01\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(171): Illuminate\Cookie\Middleware\EncryptCookies->handle(Object(Illuminate\Http\Request), Object(Closure)) #13 D:\privatePro\laravel\larave01\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(105): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request)) #14 D:\privatePro\laravel\larave01\vendor\laravel\framework\src\Illuminate\Routing\Router.php(682): Illuminate\Pipeline\Pipeline->then(Object(Closure)) #15 D:\privatePro\laravel\larave01\vendor\laravel\framework\src\Illuminate\Routing\Router.php(657): Illuminate\Routing\Router->runRouteWithinStack(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request)) #16 D:\privatePro\laravel\larave01\vendor\laravel\framework\src\Illuminate\Routing\Router.php(623): Illuminate\Routing\Router->runRoute(Object(Illuminate\Http\Request), Object(Illuminate\Routing\Route)) #17 D:\privatePro\laravel\larave01\vendor\laravel\framework\src\Illuminate\Routing\Router.php(612): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request)) #18 D:\privatePro\laravel\larave01\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(176): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request)) #19 D:\privatePro\laravel\larave01\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(130): Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\{closure}(Object(Illuminate\Http\Request)) #20 D:\privatePro\laravel\larave01\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\TransformsRequest.php(21): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request)) #35 {main} "}
回答2件
あなたの回答
tips
プレビュー