using RestTemplate.exchange GET request vs posting URL in google chrome - java

Rest Template is used to create applications that consume RESTful Web Services. You can use the exchange() method to consume the web services for all HTTP methods.

Chrome (or any browser) will send several extra HTTP headers along with the request, such as user agent, cookies, etc, by default. It'll also render any Javascript from the response as well as make extra requests for static assets such as CSS files, fonts, images, etc. Unclear what you mean by "url posting", but POST requests are typically only done on HTML forms.
RESTTemplate will only send headers and session data you've explicitly set, and will not evaluate any Javascript content in responses. It'll return the plaintext returned from the web server, and not request files linked within an HTML response.
An "image response" would be returned as bytes for both, potentially with a Content-Type header. RESTTemplate cannot "render" HTML <img> tags. Instead, you'd need to parse the src address, then make a new GET request for static assets

Related

Read POST form in Angular submitted by external websites

I am developing a website with Java for the backend and Angular for frontend. There is a situation when some external websites may send data to my website using POST form. For instance,
▼ General
Request URL: https://myangularwebsite/
Request Method: POST
...
▼ Request Headers
Content-Type: application/x-www-form-urlencoded
Host: myangularwebsite
Origin: https://externalwebsite
Referer: https://externalwebsite/send.form?id=0
...
▼ Form data
ID: 0000000
TIME: 2017.06.04 11:53:58
SIGNATURE: ...geirgmGKFGJWR...
...
Now, I need to capture the form in Angular somehow, send/redirect it to the backend to validate the signature and return the answer back to Angular to proceed working with this website.
I tried posting to my website to test how it might work using Postman, but get Cannot POST /.
I know how to work with GET and URL query parameters in Angular but I think I need to process a POST request based on headers I see with Chrome DevTools 'Network' section when coming from externalwebsite to myangularwebsite.
Should I dedicate a route in the backend and expose it, for example, .../api/external in my backend and tell these websites to use this link instead of directly posting to my Angular website's homepage?
I have already read another question ( How to read form post data in Angular 2 typescript? ) which is somewhat similar but I do not think using PHP is the right way for me as the website I am developing already has an older version written in PHP.
The answer at the link you provided is correct: you cannot do it in just Javascript, you have to use some server-side code. They mention PHP as an example, but any server-side component will do, and as you have Java at your backend, let it be Java.
So, when an HTTP request comes from an external site, you have to use a server-side component to handle it. But there are some options.
If this request is made using your user browser (so it is something like a redirect, but using a POST method), then you can do the following: catch that request at your backend, output some javascript with some data to the user's browser and process that data in your Angular code. Or this could be a redirection to your main Angular entry point, it is up to you.
If this request is made by some other means (for example, this is a server-to-server request made with with curl like a notification from a Credit Card processing), with no browser involved, then you don't need to have any Javascript (Angular or whatever it could be) as they are needed for browser only. In this case you just handle the request at your server-side.
In both cases, it seems plausible to dedicate some special endpoint for handling (or landing) such externally-originated requests.

HTTPS using HTTP POST

I'm currently programming for Android 4.4 and I'm wanting to fill a web form with a username and password combination. This is obviously sensitive data, and I'm connecting to a HTTPS web page.
Is using the HTTP POST method sensible and encouraged for this or is there an alternate method I can use to do it?
HTTP Post vs HTTP GET
in GET method you send the parameters part of the url string
in POST method you send the parameters part of the post body
HTTP vs HTTPS
in HTTP you send the request as plain text, very readable by 3rd party applications
in HTTPS you encrypt the contents of the request, and thus the 3rd party applications would need some additional keys to decrypt it.
the question of sensitivity depends on who do you want to hide the information from?
if from the user, HTTP POST is enough, the average user does not try to read the contents of http requests.
if from hackers, then you'd need HTTPS

How to display restful request and response on the browser. My client wants to debug the request and response from client application

I created a swing client application which calls Restful api. I want to display both request and response. I could display response but request since it is a URI i could not print it in a xml format. How to display the request
There are plenty of browser plugins that'll let you construct a request to RESTful services. My favorite is Postman for Chrome
It'll let you type in the URI, required headers, and body, and displayes the results. Very useful for debugging RESTFul services.
I have found RESTClient to be nice. Its available in both chrome and firefox.
https://addons.mozilla.org/en-us/firefox/addon/restclient/
https://chrome.google.com/webstore/detail/advanced-rest-client/hgmloofddffdnphfgcellkdfbfbjeloo
Personally I've only used the one on Firefox since there's where I do any of my debugging.

Send http headers from servlet to app in another environment

I need to send a request from a servlet to an application running in other environment (IIS) with certain information in custom headers.
I know redirecting doesn't send the headers and getServletContext().getRequestDispatcher(url) is to be used in the servlet's context only. Has anybody made this work in some other way? I was thinking in using HttpURLConnection, but would it finally redirect the browser to the targeted app ?
Thanks in advance
You can't redirect from your servlet to the external server and keep the headers, and you can't forward the incoming request to other context.
What you could do, is to use the HttpURLConnection (or other http client library such as HTTPClient) to make the request (with the custom headers you need) to the remote server and, once the operation is complete, redirect the user to the external site (customm headers are not set in this redirection).
This is a little tricky, if you elaborate your question (what do you really need to do) we can probably think about other alternatives.
AJAX? You can send some JavaScript code to the browser which sends a request to the ISS and handle (shows) its answer. It's possible to set http headers with XMLHttpRequest but it needs client side JavaScript coding and you have to find a way to send cross domain requests.

java socket to read content of webpage

Is it possible to use java socket API to read content of a webpage, ex: "www.yahoo.com"? Can somebody here show an example?
And how about reading content of a page protected by the web app login screen?
Thanks in advance,
dara kok
It's possible but not advisable. The webpage is returned using HTTP, which is more than just a stream of bytes. This means that in order to use a socket you application would need to understand the instructions in the HTTP responses and behave accordingly.
To programitically access a webpage use Jakarta Commons HTTP Client.
With regards to secure webpages, it will depend on how they are secured, however given HTTP Client can maintain cookies you should be able to perform the login through code too.
Further to Nick's answer (i.e. use the Jakarta commons HTTP Client). The login security depends on how the login page is implemented, if it is an apache .htaccess secured site you will need to place username/password information in the request header. Alternatively (and generally more usual), if it is an html form, you will need to deconstruct the form fields from the original HTML and send those as key/value parameters in the http GET/POST request

Categories