make RESTful API call ONLY by calls in java.io - java

I was asked is there anyway to call RESTful API only by java.io. No java.net URL and 3rd party library. I did try to open HTTP by different reader/InputStream under java.io, it seems getting FileNotFoundExceptions, etc.
Any ideas?

According to the Java documentation:
Package java.io
Provides for system input and output through data streams,
serialization and the file system.
In order to make a REST call using such such approach, you may write a REST method call into a JSON format and payload into a File then have a another application read the file that will then do the actual REST API call.
And since this is not a direct REST call, the answer to your question is that it's not possible not to use any networking module of the Java Platform to achieve what you require.
Although I wonder why you are restricted to use only java.io for REST API calls?

I was thinking it can make RESTful call indirectly via OS command, e.g. compose curl script and use Process to execute it, it redirect output to a file and then read the output file to parse output JSON

Related

How to get specific information from a website using JAVA HTTP request

I want to write a program in java to make http request and print for me only the information I Want. For example there's this website: "https://br.investing.com/currencies/usd-brl"
And here I need to get only the information about the dolar value in real time. How do I do that in java? I thought it would be more easy using an API but I have no idea on how to do it.
I have no idea where to start or resources i should be using, I need to use only native java resources no third party modules.
there isn't API at 'investing.com' but simple widget. you should crawl the data OR using API via another site.
i think the Exchange rate information is provided by not only investing.com but also several finance service. And you can using Class URL, HttpUrlConnection in java.net to request for data to target url you need.
https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/net/URL.html

Java: Send GET PUT POST to an external API and get response

I am relatively new to JAVA and am trying to build a back-end application which will call another server where the data is present.
I have to do GET, POST, PUT and PATCH(can live without PATCH) operations to an external server in the intranet. I can hit the exposed service using postman app (using custom header)but want to do the similar action from my Java code, get the output in my Tomcat Server.
I have limitations in modifying dependencies to the POM file or adding jars to the library, so am looking for a solution other than Jersey.
You can use HttpURLConnection (which extends URLConnection) to handle the http requests and these classes are part of JDK itself so you don't need any external jar files, I suggest you look here for basics on these and look here for examples.
Most of the places, I look for provides me the answer for GET. I see
some POST too, but my requirement is mainly for PUT.
In order to place PUT, DELETE, etc.. http operations, you can use httpConnectionObj.setRequestMethod("PUT") or httpConnectionObj.setRequestMethod("DELETE") as given in an example here.
you could set up a simple java.net.Socket instance and implement the calls yourself - then you don't need any dependencies.
http://www.cafeaulait.org/course/week12/22.html
java.net.HttpURLConnection seems to be the way to go then. This might i.e. be initialized like so: (HttpURLConnection) new URL(url).openConnection()

HTTP(S) request to another server using Java tools?

I'm new to this and I need my Java program to send http or https requests on the different websites(for example, Amazon)
so it can get the HTML code and than I would be able to find information(price on some goods) that I need via Pattern class.
Did anybody faced with that task? Maybe you used JSON or other tools? Thank you.
It seems that Amazon have an API. You should using instead of trying to parse their website.
Regarding libs to call webservices in JAVA, you could use Retrofit.
There are several parts to what you are asking:
Constructing / determining what to include in the HTTP request
Issuing the actual HTTP request
Parsing the response
The first and last are dependent on the particular service / API you are invoking, though if the API response is in a standard format (e.g. JSON), there are libraries that can help you interpret the response (though exactly which fields in the response mean something to you, will depend on the particular API and your application). Issuing the HTTP request, itself, is something that can be done with a number of different libraries, including the builtin HttpURLConnection / URL classes, as well as third party libraries such as the Apache HttpComponents or the Google HTTP Java Client Library, the latter of which includes libraries for parsing common output formats, as well.

is it possible to use php api function in javajet code?

I want to create a Talend component that based on javajet code, that talend component fetch data from one web based inventory system. But the thing is that the api of that system is available in php so I want to use that php api function in javajet code. is it possible?
The .javajet code generates plain Java, so you cannot embed or call PHP directly in that. But you should be able to call your PHP API by invoking the PHP interpreter as a subprocess in the generated code, e.g. by calling
Runtime.getRuntime().exec("php myapi.php").
You would then need to come up with a mechanism to pass arguments and results to/from the subprocess, preferably via command-line arguments and/or standard input and standard output.
Alternatively, you might want to invoke your PHP API through a web server, which means you would have to issue an HTTPRequest to that webserver and parse the response you get from the server.

How can I implement Hunch's API in java?

I'm creating a Java application that uses Hunch's API, but the problem is I have no clue as to how I can use a web API in Java. Any ideas?
That depends on what the API provides, generally you'd need a web server (like Tomcat) to run a web framework.
If it's just an api to communicate with a service via HTTP (maybe webservices) then have a look at the corresponding libraries (JAX-WS etc.).
Edit: I looked up Hunch API, and assume it is this: http://hunch.com/developers/v1/
It seems like it would return JSON encoded data, so you might use the URL class to read data from an URL. That data would be a JSON string which then needs to be decoded using one of the many JSON libraries out there (e.g. JSON-simple).

Categories