简体   繁体   中英

How to pass method name as query string in URL when executing REST api?

I am working with REST api's and I want to test these API's by calling them in a REST client (I am using postman)

http://localhost/flat_booking_again/api/api.php

Above is the url where all my api's reside, I want to call a particular api by passing the api name in the url like this

http://localhost/flat_booking_again/api/api.php?method=insertBooking

Cany anyone help me out on how I can achieve this?

I am fairly new to using web services api's pardon me if this question seems silly. Any help is greatly appreciated. Thanks !

You have misunderstood what REST API is. You should probably go ahead and read it. This video will help you with that.

In REST API you do not use method name to insert a booking.

You would perform a PUT request to http://localhost/api/bookings to insert a booking.

The REST API is convention based and are triggered by various HTTP methods.

If you want to work with a booking object, then you would have a api endpoint as follows:

http://localhost/api/bookings

Then you would use HTTP methods to trigger actions on the endpoint.

PUT    - /bookings/1    - Update booking with id 1
POST   - /bookings      - Create new booking
DELETE - /bookings/1    - Remove a booking with id 1
GET    - /bookings/1    - Get booking with id 1
GET    - /bookings      - List all booking

You can use some REST API library to create implement your routes and api.

the rest API has not the same philisophy as a SOAP WS.

In the case of REST architecture, you must think your service as CRUD service. Create is equivalent to POST verb Read is do with the GET verb. Update => PUT Delete => delete.

And in your case, your URL msut design your object and not your service. The notion of method desapears in profit of object oriented.

see this example: https://en.wikipedia.org/wiki/Representational_state_transfer

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