0

I want to use JSON as input and output format for the API's.

I am able to create webservices with HTTP POST method and Input format as JSON, but I am not able to do the same with HTTP GET request.

So, is it even possible to use JSON Input format for GET request ?

jcoder
  • 73
  • 1
  • 1
  • 5
  • You can but you should not. See also [HTTP GET with request body](http://stackoverflow.com/a/983458/1353722) – lefloh Sep 15 '14 at 10:17
  • Yes, I also came across that yesterday but as you said too I didn't wanted to go with this approach. Thanks – jcoder Sep 15 '14 at 15:01

1 Answers1

1

Typically a GET request is used to retrieve a resource, it's not used to create or update it. That means you typically don't have to send a lot of parameters. In most cases you will send only an id as part of the URL (something like: https://myAPI.com/products/123 to retrieve product with id 123 for example) RestEasy allows you to read parts of the URL using @PathVariable. This link gives a quick summary on best practices: http://www.restapitutorial.com/lessons/httpmethods.html

Birgit Martinelle
  • 1,869
  • 1
  • 12
  • 9
  • I agree with you on this but as I am using JSON input format for POST requests, my response format is JSON so thought if the GET requests are designed with JSON input format then it would be consistent throughout the app. So, is it a good practice to have such structure? – jcoder Sep 15 '14 at 02:19
  • The idea is that the response to your GET request does return JSON - but the request to retrieve the resource (the GET request) does usually not contain any information other than an Id as part of the query string (maybe some filtering) – Birgit Martinelle Sep 15 '14 at 02:26
  • What are you trying to accomplish with your GET request? – Birgit Martinelle Sep 15 '14 at 02:27
  • Sorry, I was off for the day so late reply. With GET requests I will be retrieving the resources. I will go with the option of QueryParameter. Thanks. – jcoder Sep 15 '14 at 14:59
  • I suggest you can use JSON in either query parameters or Path parameters, if parameter indicates unique identity go for path, if parameter indicates filtering/searching then use that in query params. Base64 Encode the JSON before putting it in URL to avoid unnecessary URL encoding of your data. – dvsakgec Sep 16 '16 at 08:50