Implement Fetch API in Nashorn!
With this Complete you will be able to Run fetch in Java with Nashorn!
Credits to the bookstore of:
nashorn-polyfill
First you have to run the JavaScript file with Nashorn from Java lib/GlobalAPI.js and Creadores Program/Fetch API.js and then upload your code and you are ready to use the Fetch API!
The API is Slightly different than the Browser but it does the same here an explanation:
//How to make a GET request
var requestGET = fetch("https://theurltest.com", "GET");
console.info("The page say: " + requestGET);
//It also works without putting "GET" example:
var requestGET2 = fetch("https://theurltest.com");
console.info("The page say: " + requestGET2);
//How to make POST request
var requestPOST = fetch("https://theurltest.com", "POST", JSON.stringify({
content: "Content you send to the server, You can also obviously add more variables as you need just like Fetch!"
}));
//If the server sends a message you can receive the message!
console.info("The server says: " + requestPOST);
Here is an explanation of the Function:
In GET it is not necessary since you can use fetch("URL");
In post it is like this:
//how fetch works:
fetch(theurl, method, contentToSend);
specify the content type in parameter 4, and in the parameter 5 extra healders like Authorization, etc. in Json example: { Authorization: "hepi" }