Basic Syntax

  • HTTP client sends requests and retrieves a response
  • A response body will be received if the request is successful
  • The JSON data from the API can formatted for our needs
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

HttpRequest request = HttpRequest.newBuilder()
		.uri(URI.create("https://contextualwebsearch-websearch-v1.p.rapidapi.com/api/Search/ImageSearchAPI?q=dream%20face%20reveal&pageNumber=1&pageSize=10&autoCorrect=true"))
		.header("X-RapidAPI-Key", "ba3cd3168dmsh90f3dc264426f1bp1aa7f3jsne8dc8f45e66c")
		.header("X-RapidAPI-Host", "contextualwebsearch-websearch-v1.p.rapidapi.com")
		.method("GET", HttpRequest.BodyPublishers.noBody())
		.build();
HttpResponse<String> response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());

System.out.println(response.body());
{"_type":"images","totalCount":1178,"value":[{"url":"https://editors.dexerto.com/wp-content/uploads/2022/10/02/danny-gonzales-dream-face-reveal.jpg","height":675,"width":1200,"thumbnail":"https://rapidapi.usearch.com/api/thumbnail/get?value=3818080606879595940","thumbnailHeight":126,"thumbnailWidth":224,"base64Encoding":null,"name":"","title":"Dream fans demand Facetime with Danny Gonzalez amid face reveal campaign - Dexerto","provider":{"name":"dexerto","favIcon":"","favIconBase64Encoding":""},"imageWebSearchUrl":"https://usearch.com/search/dream%20face%20reveal/images","webpageUrl":"https://www.dexerto.com/entertainment/dream-fans-demand-facetime-with-danny-gonzales-amid-face-reveal-campaign-1947806/"},{"url":"https://lezeto.s3.us-east-2.amazonaws.com/wp-content/uploads/2021/12/01091052/Dream-Face-Reveal-Drama.jpg","height":432,"width":768,"thumbnail":"https://rapidapi.usearch.com/api/thumbnail/get?value=7108681215082112798","thumbnailHeight":108,"thumbnailWidth":192,"base64Encoding":null,"name":"","title":"The Truth About Dream Face Reveal Drama - Lezeto.com","provider":{"name":"lezeto","favIcon":"","favIconBase64Encoding":""},"imageWebSearchUrl":"https://usearch.com/search/dream%20face%20reveal/images","webpageUrl":"https://www.lezeto.com/the-truth-about-dream-face-reveal-drama/"},{"url":"https://shotoe.com/uploads/news/28/2855/1/2855173-everything-dream-has-said-about-their-face-reveal-plans.jpg","height":319,"width":544,"thumbnail":"https://rapidapi.usearch.com/api/thumbnail/get?value=422798834116271533","thumbnailHeight":119,"thumbnailWidth":202,"base64Encoding":null,"name":"","title":"Everything Dream Has Said About Their 'Face Reveal...  Dream  Shotoe","provider":{"name":"shotoe","favIcon":"","favIconBase64Encoding":""},"imageWebSearchUrl":"https://usearch.com/search/dream%20face%20reveal/images","webpageUrl":"https://shotoe.com/dream-st_275423/news/everything-dream-has-said-about-their-face-reveal-plans-sn_2855173/"},{"url":"https://www.svg.com/img/gallery/dream-knows-how-he-wants-to-do-a-face-reveal/l-intro-1623261532.jpg","height":900,"width":1600,"thumbnail":"https://rapidapi.usearch.com/api/thumbnail/get?value=8835138760831175764","thumbnailHeight":126,"thumbnailWidth":224,"base64Encoding":null,"name":"","title":"Dream Knows How He Wants To Do A Face Reveal","provider":{"name":"svg","favIcon":"","favIconBase64Encoding":""},"imageWebSearchUrl":"https://usearch.com/search/dream%20face%20reveal/images","webpageUrl":"https://www.svg.com/432967/dream-knows-how-he-wants-to-do-a-face-reveal/"},{"url":"https://static3.gamerantimages.com/wordpress/wp-content/uploads/2021/01/dream-face-reveal.jpg","height":700,"width":1400,"thumbnail":"https://rapidapi.usearch.com/api/thumbnail/get?value=140615080090905572","thumbnailHeight":98,"thumbnailWidth":196,"base64Encoding":null,"name":"","title":"Popular YouTuber Dream Has Been Doxed | Game Rant","provider":{"name":"gamerantimages","favIcon":"","favIconBase64Encoding":""},"imageWebSearchUrl":"https://usearch.com/search/dream%20face%20reveal/images","webpageUrl":"https://gamerant.com/dream-face-reveal-youtube-dox/"},{"url":"https://www.dexerto.com/wp-content/uploads/2022/03/30/dream-face-reveal-when.jpg","height":900,"width":1600,"thumbnail":"https://rapidapi.usearch.com/api/thumbnail/get?value=3063018545565945965","thumbnailHeight":225,"thumbnailWidth":400,"base64Encoding":null,"name":"Dream face reveal teaser","title":"Dream face reveal: Will Dream finally show his face after more teasers? - Dexerto","provider":{"name":"dexerto","favIcon":"","favIconBase64Encoding":""},"imageWebSearchUrl":"https://usearch.com/search/dream%20face%20reveal/images","webpageUrl":"https://www.dexerto.com/entertainment/dream-face-reveal-1794663/"},{"url":"https://staticg.sportskeeda.com/editor/2022/10/30084-16647305086199-1920.jpg","height":1080,"width":1920,"thumbnail":"https://rapidapi.usearch.com/api/thumbnail/get?value=6329458979250092495","thumbnailHeight":135,"thumbnailWidth":240,"base64Encoding":null,"name":"Dream trending on Twitter after teasing an early face reveal (Image via Sportskeeda)","title":"\"Proud of Dream\" trends on Twitter as fans celebrate journey of Minecraft star before his official face-reveal","provider":{"name":"sportskeeda","favIcon":"","favIconBase64Encoding":""},"imageWebSearchUrl":"https://usearch.com/search/dream%20face%20reveal/images","webpageUrl":"https://www.sportskeeda.com/esports/news-proud-dream-trends-twitter-fans-celebrate-journey-minecraft-star-official-face-reveal"},{"url":"https://famouspeopletoday.com/wp-content/uploads/2021/05/ChrisFix-Net-Worth-Face-Reveal-Real-Name-Biography.jpg","height":805,"width":1210,"thumbnail":"https://rapidapi.usearch.com/api/thumbnail/get?value=6606051994259958598","thumbnailHeight":402,"thumbnailWidth":604,"base64Encoding":null,"name":"","title":"ChrisFix - Net Worth, Face Reveal, Real Name, Biography - Famous People Today","provider":{"name":"famouspeopletoday","favIcon":"","favIconBase64Encoding":""},"imageWebSearchUrl":"https://usearch.com/search/dream%20face%20reveal/images","webpageUrl":"https://famouspeopletoday.com/chrisfix/"},{"url":"https://staticc.sportskeeda.com/editor/2022/10/123da-16647288760781-1920.jpg","height":1081,"width":1920,"thumbnail":"https://rapidapi.usearch.com/api/thumbnail/get?value=362576054151781025","thumbnailHeight":135,"thumbnailWidth":239,"base64Encoding":null,"name":"Minecraft fame Dream teases much-awaited face reveal  (Image via Sportskeeda)","title":"Why Dream's face-reveal means so much to Minecraft community","provider":{"name":"sportskeeda","favIcon":"","favIconBase64Encoding":""},"imageWebSearchUrl":"https://usearch.com/search/dream%20face%20reveal/images","webpageUrl":"https://www.sportskeeda.com/esports/why-dream-s-face-reveal-means-much-minecraft-community"},{"url":"https://staticc.sportskeeda.com/editor/2022/10/6c2c4-16647164620710-1920.jpg","height":1081,"width":1920,"thumbnail":"https://rapidapi.usearch.com/api/thumbnail/get?value=27269792429543382","thumbnailHeight":135,"thumbnailWidth":239,"base64Encoding":null,"name":"Popular Minecraft YouTuber Dream has fans riled up as they await his face reveal (Image via Twitter)","title":"Why is 'Dream face reveal' trending on Twitter?","provider":{"name":"sportskeeda","favIcon":"","favIconBase64Encoding":""},"imageWebSearchUrl":"https://usearch.com/search/dream%20face%20reveal/images","webpageUrl":"https://www.sportskeeda.com/esports/why-dream-face-reveal-trending-twitter"}]}

Web Service

We will be using JavaScript in our application for easier compatibility with HTML and CSS. Below is a web service written in JavaScript for a search API.

function ApiRequest() {
	const http = require("https");
	
	const options = {
		"method": "GET",
		"hostname": "contextualwebsearch-websearch-v1.p.rapidapi.com",
		"port": null,
		"path": "/api/Search/ImageSearchAPI?q=taylor%20swift&pageNumber=1&pageSize=10&autoCorrect=true",
		"headers": {
			"X-RapidAPI-Key": "ba3cd3168dmsh90f3dc264426f1bp1aa7f3jsne8dc8f45e66c",
			"X-RapidAPI-Host": "contextualwebsearch-websearch-v1.p.rapidapi.com",
			"useQueryString": true
		}
	};
	
	const req = http.request(options, function (res) {
		let str = "";
	
		res.on("data", function (chunk) {
			str += chunk;
		});
	
		res.on("end", function () {
			parsedData = JSON.parse(str);

			// print first hit in data
			console.log(parsedData['value'][0])
		});
	});
	
	req.end();
}

ApiRequest();
{ url: 'http://cache.umusic.com/_sites/_halo/taylorswift/images/meta-image.jpg',
  height: 630,
  width: 1200,
  thumbnail: 'https://rapidapi.usearch.com/api/thumbnail/get?value=1094927124898698511',
  thumbnailHeight: 157,
  thumbnailWidth: 299,
  base64Encoding: null,
  name: '',
  title: 'Taylor Swift - Official Website',
  provider: { name: 'umusic', favIcon: '', favIconBase64Encoding: '' },
  imageWebSearchUrl: 'https://usearch.com/search/taylor%20swift/images',
  webpageUrl: 'https://www.taylorswift.com/' }