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

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!

Related

How to put parameters in URL using Java and httpclient?

I am really new to Http stuffs and am trying to put parameters in the URL in order to make some requests from a certain API.
For example, the guide for the API says that we need to do
https POST api_site.io/users/$username/token,
but using Java, how do we pass the username parameter before /token and do a request for POST?
This is the start and I have a lot more things to do for the next but if someone enlightens the way of how to start, I think can do the other things by myself.
Thanks in advance!

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

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?

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!!!

What's the best way to let the Ajax app know of the errors back at server?

Hi
I'm working on an application with Java as it's server-side language and for the client-side I'm using Ajax.
But I'm fairly new to ajax applications so I needed some opinions on the issue I've faced.
I'm using Spring Security for my authentication and authorization services and by reading spring forums I've managed to integrate Spring Security with Ajax application in a way that ajax requests can be intercepted and relevant action be taken.
Here's the issue: What is the best way to let the ajax application know that an error has occurred back at server. What I've been doing so far is that by convention I make random http 500+ errors. e. g. to prompt for login I return 550, and 551 for other issue and so forth. But I think this is not the right approach to this. What is the best approach for dealing with this situation?
If standard HTTP error codes (eg 401 Unauthorized) are rich enough, use them. Best not to make up your own HTTP error codes, they're meant to be fixed. If you need more info to be returned, you should return a richer object in the response body (serialized as eg JSON or XML) and parse the object on the client side.
In my experience, making up your own HTTP error codes is not the best approach.
I've known client and server-side HTTP protocol stacks to treat non-standard HTTP status codes as protocol errors.
A non-standard code is likely to lead to confusing error messages if they end up being handled as non-AJAX responses.
Similarly, using the "reason phrase" part of the response can be problematic. Some server-side stacks won't let you set it, and some client-side stacks discard it.
My preferred way of reporting errors in response to an AJAX request is to send a standard code (e.g. 400 - BAD REQUEST) with an XML, JSON or plain text response body that gives details of the error. (Be sure to set the response content type header ...)
If this a bug in your application or a hack that you want to protect from, just return a generic access error. Don't give detail of the error on the client as it could be used by the hacker to better understand how to abuse your API. This would confuse normal users anyway.
If this is to be normal application behaviour, it might be better to be sure that you fail gracefully by allowing to retry later (if it make sence), reconnect or reauthenticate. You should at least recognise if it is a disconnected error or an insuffiscient rights error, and display a nice looking explanation to the user.

How to modify the header of a HttpUrlConnection

Im trying to improve the Java Html Document a little but i'm running into problems with the HttpUrlConntion. One thing is that some servers block a request if the user agent is a Java VM. Another problem is that the HttpUrlConnection does not set the Referrer or Location header field. Since several sites use these fields to verify that the content was accessed from their own site, I'm blocked here as well. As far as I can see the only resolution is to replace the URL handler of the HTTP protocol. Or is there any way to modify the default HTTP Handler?
Open the URL with URL.openConnection. Optionally cast to HttpURLConnection. Call URLConnection.setRequestProperty/addRequestProperty.
The default User-Agent header value is set from the "http.agent" system property. The PlugIn and WebStart allow you to set this property.
If you use Apache HttpClient to manage your programmatic HTTP connectivity you get an extremely useful API which makes creating connections (and optional automatic re-connecting on fail), setting Headers, posts vs gets, handy methods for retrieving the returned content and much much more.
I solved my problem. We can just send the header to application/json and pass the body as a json object. That simply solves the issue.

Categories

Resources