简体   繁体   中英

Problems with curl requests for RESTful API - Laravel 5.4

I am trying to create a RESTful application using Laravel (I am beginner). It is a login-based app with creating a post and creating a comment to a post.(only authenticated users can write a post or comment). I made the functionality to create post and I tested it with curl request that looks like this:

$ curl --data "title=test&body=test" http://127.0.0.1:8000/api/post?api_token=dLE6IofaGHmG1CWU6b6jWUE9h4161G8FJ3719CjZkTRbkH4orIvT5aGGNhWt

It is working and everything is fine. I also created the routes in api.php for login/logout/register. And I want to test them via curl requests. So I am trying to test it with some requests like:

$ curl --data "name=test&email=test@gmail.com&password=test&password_confirmation=test" http://127.0.0.1:8000/api/register

And they all redirect me to login. Can you tell me where I am wrong?

Thanks in advance.

routes/api.php does not use the StartSession middleware and since logins use sessions, it will always prompt you to login on each request.

You have a few options

1) move your API routes into routes/web.php so your API uses sessions.

This is not the best design however as APIs should be stateless to follow good REST design principles, but it will solve your login problem.

2) Look into Laravel Passport as a commenter above suggested.

This would be the preferred method, especially if you plan on consuming your API from multiple platforms.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM