REST Web Services

Best Practices to follow while writing REST WS :

  1. http://www.vinaysahni.com/best-practices-for-a-pragmatic-restful-api
  2. http://blog.mwaysolutions.com/2014/06/05/10-best-practices-for-better-restful-api/

Form encoding while making POST requests :

When you make a POST request, you have to encode the data that forms the body of the request in some way.

HTML forms provide three methods of encoding.

  • application/x-www-form-urlencoded (the default)
  • multipart/form-data
  • text/plain

Use multipart/form-data if the request contains a File upload and it should be used in combination with the INPUT element, type=”file”.
Use application/x-www-form-urlencoded otherwise, which is the default if you omit enctype header in the request.
NEVER use text/plain as it only used if you want to debug the payload you are sending as part of HTTP request.

URL Encode Query params : When sending requests to REST API from POSTMAN, make sure you URL-Encode the query parameters part of the request.

CORS : Cross origin Resource Sharing : (https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS)
When a request is made from browser to fetch data from an API, Origin header is always sent along with the request. Origin denotes the source of the request. This origin is parsed and verified at API to determine if it can respond with data.

For Http requests that can cause side effects on server data like PUT, DELETE, browser sends an OPTIONS request(Preflight request) before sending the actual request. When API gets the OPTIONS request, it then responds with details like allowed methods, allowed headers, etc – only then browser makes the actual PUT/DELETE requests to API.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s