java rest web services - side effect of using get instead of post - java

What is the side effect of using get method in java rest web service instead of post or put or delete? I know I have to use get for retrieving data and post for saving. But, what if I used get instead?

You can but you will probably going against the design. There are many good discussion here and below are few references,
HTTP GET with request body
REST API using POST instead of GET
REST: Can I use POST request to read data?

Related

Magento REST API details or swagger doc

How to find the correct json schema to post to the magento endpoint and create an account?
Am able to create account only from the front end, I need to be able to do from the backend using curl or Postman.
http://{ipaddress}/index.php/customer/account/create/
This is an external api , i need to consume.
I want to post to this end point from java, however am not able to find the correct request schema of the json endpoint.
Any help to find the swagger end point or schema would be great
I am assuming you are talking about magento 1? Have you tried the REST API by chance? It's well written out on how you should make a call to it here.
If you need more functionality than just creating an account, you can also take a look at the SOAP API which handles more things, but does not support JSON from the box. They did implement the WS-I complaint mode to support Java calls, so maybe that's something you'd be interested in as well.
Good luck!

Simulate form post using http client in Android app?

So, I'm currently developing an app for a service which has a json-based (unfortunately) read only API. Retrieving content is no problem at all, however the only way to post content is using a form on their site which location is a PHP script. The service is open source so I know which fields the form expects, but whatever I send, it always results in a BAD REQUEST.
I captured the network traffic inside my browser and as far as I can see, the browser constructs a multipart form request, however when I copy the request and invoke it again using a REST client, a BAD REQUEST gets returned.
Is there a way to construct a http request in Android that simulates a form post?
If it's readonly I think you wouldn't be able to make requests with POST (it's assume for editing or adding things).
If you let me make you an advise, I recommend you using this project as a Library.
https://github.com/matessoftwaresolutions/AndroidHttpRestService
It makes you easy deal with apis, control network problems etc.
You can find a sample of use there.
You only have to:
Build your URL
Tell the component to execute in POST mode
Build your JSON
As I told you, I don't know even if it will work.
I hope it helps!!!

Can I do an http GET and include a body using HttpURLConnection?

I need to perform an http GET for a REST service and include a body in the GET. Unfortunately, setting #setDoOutput( true ) on the connection forces a POST. Is there anyway to send a GET with a body?
Edit: The body I'm trying to send is JSON.
It is not possible to send content for an HTTP GET using HttpURLConnection. By setting setDoOutput(true) on an HttpURLConnection the verb is forced to be POST.
The documentation for the REST API I was using described a JSON body for the endpoint in question, but URL parameters were accepted.
It might not be possible through HttpUrlConnection, although you might be able to do it through another APIs BUT, if you have to do it that way chances that you are doing something wrong in your architecture are high because it goes against the basic usage of GET Http Method and different problems might arise like:
If you ever try to take advantage of caching, Proxies are not going to look in the GET body to see if the parameters have an impact on the response.
It's not a good implementation based on standard practices and it could cause problems with some browsers / services.
Take a look at this question for more information.
HTTP GET with request body
Hope this helps.
Regards!

Handling Post Requests

yesterday i started brainstorm for a project of mine and I'm not sure if its the correct approach.
On my website I'm having an (kind of an order form) which sends a post to a target URL, which works with a simple curl php script. The target is an external service (where I have no access no rights, nothing). I only know that I will get a POST with further processed data back from the service, which I have to save into my DB.
in steps:
Users fills out the (order) form and posts data to an external url on my website.
data gets externally handled and after finishing that resents a post.
read incoming post data.
save data into DB.
Success page on my website.
My thoughts were to handle the incoming data with a servlet (spring maven project) but I'm not sure if this is a correct approach. Is there a better why to do this. Or is the first step with the php scripts wrong. thx for any help.
A simplest workflow could be
1. Forward the initial (Order form with values) request to a servlet
2. Invoke a post request using java to an external url inside this servlet (Using Apache http client, or libraries such as HTMLUnit)
3. Once you get the incoming response in your servlet, you can update your database.
If you are using spring, the controller could forward initial request to a business class which will handle this post processing and delegate the database update to respective DAO.
There are a number of suitable ways to handle this, and the decision is largely a matter of preference and what you're familiar with. Spring can handle this sort of job quite well.
Note: Maven is a build system for Java and some other JVM languages. I recommend using it, but it's not part of Spring; what you're probably looking for is Spring MVC.

JSP backend for ExtJS

This question is about a proper architecture using JSP as a controller for ExtJS.
I am fairly new to server side development but I am pretty familiar with ExtJS 4 and getting better with Java and SQL daily.
I am trying to create a JSP controller to write the data from stores in ExtJS. I have MSSQL database and Tomcat running on the server.
I successfully created a JSP (sqlData.jsp) that reads from the database and returns JSON data. I pass a query name to this JSP, it then looks up what the query is from a "query" table (columns: [query_id],[query_name],[query]). It then runs the query and returns the data in a JSON format - this is working fine to get data into ExtJS from a database.
To use this backend set-up I usually configure the store like this:
var store = Ext.create('Ext.data.Store', {
model: 'aModel',
proxy: {
type: 'ajax',
url: 'sqlData.jsp?queryName=aQueryName',
reader: 'json'
},
autoLoad: true
});
Somehow, I need this sqlData.jsp to also handle a store.save() call from the ExtJS framework. Which means the JSP needs to receive a POST request and then do an update based on a pile of JSON data (ExtJS sends read request as GET and write methods like store.save() are POST).
My plan was to add something in the Java to recognize whether it is a POST or GET request. Then, if it is a POST request, I would send it to a different Java method in the JSP to parse the JSON and write it to the database.
Of course I would have to change my "query" table to have another column for update/insert statements linked to the same queryName (i.e.: [query_id],[query_name],[select_query],[update_query]).
Does this backend implementation make any sense?
Anyone else use JSP and ExtJS to achieve this smoother?
I noticed that there is an api config option I can set in my proxy to specify different URLs for the different operations (READ, WRITE, DELETE, etc). Should I make a separate JSP and direct all write requests using this config instead?
Would it be wiser to add a writer: 'json' config on the proxy so that it parses before POSTING? I figured I would have to parse it in the JSP either way so I didn't think I should.
Any pointers will be much appreciated.
since your backend is Java, I would really recommend using Spring 3.0 MVC to code your backend.
JSP is not a good option for the stuff you are doing because:
the functions you write in there are not unit testable.
the functions you write in there are not reusable.
the code you write in JSP are functional in nature, not object oriented, you can't inject the services you need into your JSP.
Spring 3.0 MVC has really good synergy with ExtJS 4, namely the RESTful URL's and content negotiation.
This example shows how to integrate the two things together. http://java.dzone.com/articles/extjs-4-file-upload-spring-mvc
I would skip jsp and just go directly to servlets. i.e. implement the logic in the servlets for both returning json, and handling things like POST, PUT, etc.....
jsps are meant to be views. But in your case, your view layer is its own application running in the client. You only need the data.
The Servlet API puts allows you to handle requests, get the http method, and stream data to the response.
My advice is go with an MVC server side framework. My favorite is Grails which lets you work with JSON objects directly for both input and output. Its also super simple to with grails to read and write data to the database.

Categories

Resources