api
• 12 min read
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
//RapidAPI header https://rapidapi.com/louislefebure9/api/star-wars-characters/
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://star-wars-characters.p.rapidapi.com/46DYBV/star_wars_characters"))
.header("X-RapidAPI-Key", "bf09b94566msh22c602dfe97bd97p19c2fajsn1d42e34f8724")
.header("X-RapidAPI-Host", "star-wars-characters.p.rapidapi.com")
.method("GET", HttpRequest.BodyPublishers.noBody())
.build();
//RapidAPI request and response
HttpResponse<String> response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
//Printing out results
System.out.println(response.body());
[ { "id": 1, "mass": "77", "name": "Luke Skywalker", "gender": "male", "height": "172", "species": "Human", "eye_color": "blue", "homeworld": "Tatooine", "birth_year": "19BBY", "hair_color": "blond", "skin_color": "fair" }, { "id": 2, "mass": "75", "name": "C-3PO", "gender": "NA", "height": "167", "species": "Droid", "eye_color": "yellow", "homeworld": "Tatooine", "birth_year": "112BBY", "hair_color": "NA", "skin_color": "gold" }, { "id": 3, "mass": "32", "name": "R2-D2", "gender": "NA", "height": "96", "species": "Droid", "eye_color": "red", "homeworld": "Naboo", "birth_year": "33BBY", "hair_color": "NA", "skin_color": "white, blue" }, { "id": 4, "mass": "136", "name": "Darth Vader", "gender": "male", "height": "202", "species": "Human", "eye_color": "yellow", "homeworld": "Tatooine", "birth_year": "41.9BBY", "hair_color": "none", "skin_color": "white" }, { "id": 5, "mass": "49", "name": "Leia Organa", "gender": "female", "height": "150", "species": "Human", "eye_color": "brown", "homeworld": "Alderaan", "birth_year": "19BBY", "hair_color": "brown", "skin_color": "light" }, { "id": 6, "mass": "120", "name": "Owen Lars", "gender": "male", "height": "178", "species": "Human", "eye_color": "blue", "homeworld": "Tatooine", "birth_year": "52BBY", "hair_color": "brown, grey", "skin_color": "light" }, { "id": 7, "mass": "75", "name": "Beru Whitesun lars", "gender": "female", "height": "165", "species": "Human", "eye_color": "blue", "homeworld": "Tatooine", "birth_year": "47BBY", "hair_color": "brown", "skin_color": "light" }, { "id": 8, "mass": "32", "name": "R5-D4", "gender": "NA", "height": "97", "species": "Droid", "eye_color": "red", "homeworld": "Tatooine", "birth_year": "NA", "hair_color": "NA", "skin_color": "white, red" }, { "id": 9, "mass": "84", "name": "Biggs Darklighter", "gender": "male", "height": "183", "species": "Human", "eye_color": "brown", "homeworld": "Tatooine", "birth_year": "24BBY", "hair_color": "black", "skin_color": "light" }, { "id": 10, "mass": "77", "name": "Obi-Wan Kenobi", "gender": "male", "height": "182", "species": "Human", "eye_color": "blue-gray", "homeworld": "Stewjon", "birth_year": "57BBY", "hair_color": "auburn, white", "skin_color": "fair" }, { "id": 11, "mass": "84", "name": "Anakin Skywalker", "gender": "male", "height": "188", "species": "Human", "eye_color": "blue", "homeworld": "Tatooine", "birth_year": "41.9BBY", "hair_color": "blond", "skin_color": "fair" }, { "id": 12, "mass": "NA", "name": "Wilhuff Tarkin", "gender": "male", "height": "180", "species": "Human", "eye_color": "blue", "homeworld": "Eriadu", "birth_year": "64BBY", "hair_color": "auburn, grey", "skin_color": "fair" }, { "id": 13, "mass": "112", "name": "Chewbacca", "gender": "male", "height": "228", "species": "Wookiee", "eye_color": "blue", "homeworld": "Kashyyyk", "birth_year": "200BBY", "hair_color": "brown", "skin_color": "NA" }, { "id": 14, "mass": "80", "name": "Han Solo", "gender": "male", "height": "180", "species": "Human", "eye_color": "brown", "homeworld": "Corellia", "birth_year": "29BBY", "hair_color": "brown", "skin_color": "fair" }, { "id": 15, "mass": "74", "name": "Greedo", "gender": "male", "height": "173", "species": "Rodian", "eye_color": "black", "homeworld": "Rodia", "birth_year": "44BBY", "hair_color": "NA", "skin_color": "green" }, { "id": 16, "mass": "1,358", "name": "Jabba Desilijic Tiure", "gender": "hermaphrodite", "height": "175", "species": "Hutt", "eye_color": "orange", "homeworld": "Nal Hutta", "birth_year": "600BBY", "hair_color": "NA", "skin_color": "green-tan, brown" }, { "id": 17, "mass": "77", "name": "Wedge Antilles", "gender": "male", "height": "170", "species": "Human", "eye_color": "hazel", "homeworld": "Corellia", "birth_year": "21BBY", "hair_color": "brown", "skin_color": "fair" }, { "id": 18, "mass": "110", "name": "Jek Tono Porkins", "gender": "male", "height": "180", "species": "Human", "eye_color": "blue", "homeworld": "Bestine IV", "birth_year": "NA", "hair_color": "brown", "skin_color": "fair" }, { "id": 19, "mass": "17", "name": "Yoda", "gender": "male", "height": "66", "species": "Yoda's species", "eye_color": "brown", "homeworld": "NA", "birth_year": "896BBY", "hair_color": "white", "skin_color": "green" }, { "id": 20, "mass": "75", "name": "Palpatine", "gender": "male", "height": "170", "species": "Human", "eye_color": "yellow", "homeworld": "Naboo", "birth_year": "82BBY", "hair_color": "grey", "skin_color": "pale" }, { "id": 21, "mass": "78.2", "name": "Boba Fett", "gender": "male", "height": "183", "species": "Human", "eye_color": "brown", "homeworld": "Kamino", "birth_year": "31.5BBY", "hair_color": "black", "skin_color": "fair" }, { "id": 22, "mass": "140", "name": "IG-88", "gender": "none", "height": "200", "species": "Droid", "eye_color": "red", "homeworld": "NA", "birth_year": "15BBY", "hair_color": "none", "skin_color": "metal" }, { "id": 23, "mass": "113", "name": "Bossk", "gender": "male", "height": "190", "species": "Trandoshan", "eye_color": "red", "homeworld": "Trandosha", "birth_year": "53BBY", "hair_color": "none", "skin_color": "green" }, { "id": 24, "mass": "79", "name": "Lando Calrissian", "gender": "male", "height": "177", "species": "Human", "eye_color": "brown", "homeworld": "Socorro", "birth_year": "31BBY", "hair_color": "black", "skin_color": "dark" }, { "id": 25, "mass": "79", "name": "Lobot", "gender": "male", "height": "175", "species": "Human", "eye_color": "blue", "homeworld": "Bespin", "birth_year": "37BBY", "hair_color": "none", "skin_color": "light" }, { "id": 26, "mass": "83", "name": "Ackbar", "gender": "male", "height": "180", "species": "Mon Calamari", "eye_color": "orange", "homeworld": "Mon Cala", "birth_year": "41BBY", "hair_color": "none", "skin_color": "brown mottle" }, { "id": 27, "mass": "NA", "name": "Mon Mothma", "gender": "female", "height": "150", "species": "Human", "eye_color": "blue", "homeworld": "Chandrila", "birth_year": "48BBY", "hair_color": "auburn", "skin_color": "fair" }, { "id": 28, "mass": "NA", "name": "Arvel Crynyd", "gender": "male", "height": "NA", "species": "Human", "eye_color": "brown", "homeworld": "NA", "birth_year": "NA", "hair_color": "brown", "skin_color": "fair" }, { "id": 29, "mass": "20", "name": "Wicket Systri Warrick", "gender": "male", "height": "88", "species": "Ewok", "eye_color": "brown", "homeworld": "Endor", "birth_year": "8BBY", "hair_color": "brown", "skin_color": "brown" }, { "id": 30, "mass": "68", "name": "Nien Nunb", "gender": "male", "height": "160", "species": "Sullustan", "eye_color": "black", "homeworld": "Sullust", "birth_year": "NA", "hair_color": "none", "skin_color": "grey" }, { "id": 31, "mass": "89", "name": "Qui-Gon Jinn", "gender": "male", "height": "193", "species": "Human", "eye_color": "blue", "homeworld": "NA", "birth_year": "92BBY", "hair_color": "brown", "skin_color": "fair" }, { "id": 32, "mass": "90", "name": "Nute Gunray", "gender": "male", "height": "191", "species": "Neimodian", "eye_color": "red", "homeworld": "Cato Neimoidia", "birth_year": "NA", "hair_color": "none", "skin_color": "mottled green" }, { "id": 33, "mass": "NA", "name": "Finis Valorum", "gender": "male", "height": "170", "species": "Human", "eye_color": "blue", "homeworld": "Coruscant", "birth_year": "91BBY", "hair_color": "blond", "skin_color": "fair" }, { "id": 34, "mass": "66", "name": "Jar Jar Binks", "gender": "male", "height": "196", "species": "Gungan", "eye_color": "orange", "homeworld": "Naboo", "birth_year": "52BBY", "hair_color": "none", "skin_color": "orange" }, { "id": 35, "mass": "82", "name": "Roos Tarpals", "gender": "male", "height": "224", "species": "Gungan", "eye_color": "orange", "homeworld": "Naboo", "birth_year": "NA", "hair_color": "none", "skin_color": "grey" }, { "id": 36, "mass": "NA", "name": "Rugor Nass", "gender": "male", "height": "206", "species": "Gungan", "eye_color": "orange", "homeworld": "Naboo", "birth_year": "NA", "hair_color": "none", "skin_color": "green" }, { "id": 37, "mass": "NA", "name": "Ric Olié", "gender": "male", "height": "183", "species": "NA", "eye_color": "blue", "homeworld": "Naboo", "birth_year": "NA", "hair_color": "brown", "skin_color": "fair" }, { "id": 38, "mass": "NA", "name": "Watto", "gender": "male", "height": "137", "species": "Toydarian", "eye_color": "yellow", "homeworld": "Toydaria", "birth_year": "NA", "hair_color": "black", "skin_color": "blue, grey" }, { "id": 39, "mass": "40", "name": "Sebulba", "gender": "male", "height": "112", "species": "Dug", "eye_color": "orange", "homeworld": "Malastare", "birth_year": "NA", "hair_color": "none", "skin_color": "grey, red" }, { "id": 40, "mass": "NA", "name": "Quarsh Panaka", "gender": "male", "height": "183", "species": "NA", "eye_color": "brown", "homeworld": "Naboo", "birth_year": "62BBY", "hair_color": "black", "skin_color": "dark" }, { "id": 41, "mass": "NA", "name": "Shmi Skywalker", "gender": "female", "height": "163", "species": "Human", "eye_color": "brown", "homeworld": "Tatooine", "birth_year": "72BBY", "hair_color": "black", "skin_color": "fair" }, { "id": 42, "mass": "80", "name": "Darth Maul", "gender": "male", "height": "175", "species": "Zabrak", "eye_color": "yellow", "homeworld": "Dathomir", "birth_year": "54BBY", "hair_color": "none", "skin_color": "red" }, { "id": 43, "mass": "NA", "name": "Bib Fortuna", "gender": "male", "height": "180", "species": "Twi'lek", "eye_color": "pink", "homeworld": "Ryloth", "birth_year": "NA", "hair_color": "none", "skin_color": "pale" }, { "id": 44, "mass": "55", "name": "Ayla Secura", "gender": "female", "height": "178", "species": "Twi'lek", "eye_color": "hazel", "homeworld": "Ryloth", "birth_year": "48BBY", "hair_color": "none", "skin_color": "blue" }, { "id": 45, "mass": "45", "name": "Dud Bolt", "gender": "male", "height": "94", "species": "Vulptereen", "eye_color": "yellow", "homeworld": "Vulpter", "birth_year": "NA", "hair_color": "none", "skin_color": "blue, grey" }, { "id": 46, "mass": "NA", "name": "Gasgano", "gender": "male", "height": "122", "species": "Xexto", "eye_color": "black", "homeworld": "Troiken", "birth_year": "NA", "hair_color": "none", "skin_color": "white, blue" }, { "id": 47, "mass": "65", "name": "Ben Quadinaros", "gender": "male", "height": "163", "species": "Toong", "eye_color": "orange", "homeworld": "Tund", "birth_year": "NA", "hair_color": "none", "skin_color": "grey, green, yellow" }, { "id": 48, "mass": "84", "name": "Mace Windu", "gender": "male", "height": "188", "species": "Human", "eye_color": "brown", "homeworld": "Haruun Kal", "birth_year": "72BBY", "hair_color": "none", "skin_color": "dark" }, { "id": 49, "mass": "82", "name": "Ki-Adi-Mundi", "gender": "male", "height": "198", "species": "Cerean", "eye_color": "yellow", "homeworld": "Cerea", "birth_year": "92BBY", "hair_color": "white", "skin_color": "pale" }, { "id": 50, "mass": "87", "name": "Kit Fisto", "gender": "male", "height": "196", "species": "Nautolan", "eye_color": "black", "homeworld": "Glee Anselm", "birth_year": "NA", "hair_color": "none", "skin_color": "green" }, { "id": 51, "mass": "NA", "name": "Eeth Koth", "gender": "male", "height": "171", "species": "Zabrak", "eye_color": "brown", "homeworld": "Iridonia", "birth_year": "NA", "hair_color": "black", "skin_color": "brown" }, { "id": 52, "mass": "50", "name": "Adi Gallia", "gender": "female", "height": "184", "species": "Tholothian", "eye_color": "blue", "homeworld": "Coruscant", "birth_year": "NA", "hair_color": "none", "skin_color": "dark" }, { "id": 53, "mass": "NA", "name": "Saesee Tiin", "gender": "male", "height": "188", "species": "Iktotchi", "eye_color": "orange", "homeworld": "Iktotch", "birth_year": "NA", "hair_color": "none", "skin_color": "pale" }, { "id": 54, "mass": "NA", "name": "Yarael Poof", "gender": "male", "height": "264", "species": "Quermian", "eye_color": "yellow", "homeworld": "Quermia", "birth_year": "NA", "hair_color": "none", "skin_color": "white" }, { "id": 55, "mass": "80", "name": "Plo Koon", "gender": "male", "height": "188", "species": "Kel Dor", "eye_color": "black", "homeworld": "Dorin", "birth_year": "22BBY", "hair_color": "none", "skin_color": "orange" }, { "id": 56, "mass": "NA", "name": "Mas Amedda", "gender": "male", "height": "196", "species": "Chagrian", "eye_color": "blue", "homeworld": "Champala", "birth_year": "NA", "hair_color": "none", "skin_color": "blue" }, { "id": 57, "mass": "85", "name": "Gregar Typho", "gender": "male", "height": "185", "species": "Human", "eye_color": "brown", "homeworld": "Naboo", "birth_year": "NA", "hair_color": "black", "skin_color": "dark" }, { "id": 58, "mass": "NA", "name": "Cordé", "gender": "female", "height": "157", "species": "Human", "eye_color": "brown", "homeworld": "Naboo", "birth_year": "NA", "hair_color": "brown", "skin_color": "light" }, { "id": 59, "mass": "NA", "name": "Cliegg Lars", "gender": "male", "height": "183", "species": "Human", "eye_color": "blue", "homeworld": "Tatooine", "birth_year": "82BBY", "hair_color": "brown", "skin_color": "fair" }, { "id": 60, "mass": "80", "name": "Poggle the Lesser", "gender": "male", "height": "183", "species": "Geonosian", "eye_color": "yellow", "homeworld": "Geonosis", "birth_year": "NA", "hair_color": "none", "skin_color": "green" }, { "id": 61, "mass": "56.2", "name": "Luminara Unduli", "gender": "female", "height": "170", "species": "Mirialan", "eye_color": "blue", "homeworld": "Mirial", "birth_year": "58BBY", "hair_color": "black", "skin_color": "yellow" }, { "id": 62, "mass": "50", "name": "Barriss Offee", "gender": "female", "height": "166", "species": "Mirialan", "eye_color": "blue", "homeworld": "Mirial", "birth_year": "40BBY", "hair_color": "black", "skin_color": "yellow" }, { "id": 63, "mass": "NA", "name": "Dormé", "gender": "female", "height": "165", "species": "Human", "eye_color": "brown", "homeworld": "Naboo", "birth_year": "NA", "hair_color": "brown", "skin_color": "light" }, { "id": 64, "mass": "80", "name": "Dooku", "gender": "male", "height": "193", "species": "Human", "eye_color": "brown", "homeworld": "Serenno", "birth_year": "102BBY", "hair_color": "white", "skin_color": "fair" }, { "id": 65, "mass": "NA", "name": "Bail Prestor Organa", "gender": "male", "height": "191", "species": "Human", "eye_color": "brown", "homeworld": "Alderaan", "birth_year": "67BBY", "hair_color": "black", "skin_color": "tan" }, { "id": 66, "mass": "79", "name": "Jango Fett", "gender": "male", "height": "183", "species": "Human", "eye_color": "brown", "homeworld": "Concord Dawn", "birth_year": "66BBY", "hair_color": "black", "skin_color": "tan" }, { "id": 67, "mass": "55", "name": "Zam Wesell", "gender": "female", "height": "168", "species": "Clawdite", "eye_color": "yellow", "homeworld": "Zolan", "birth_year": "NA", "hair_color": "blonde", "skin_color": "fair, green, yellow" }, { "id": 68, "mass": "102", "name": "Dexter Jettster", "gender": "male", "height": "198", "species": "Besalisk", "eye_color": "yellow", "homeworld": "Ojom", "birth_year": "NA", "hair_color": "none", "skin_color": "brown" }, { "id": 69, "mass": "88", "name": "Lama Su", "gender": "male", "height": "229", "species": "Kaminoan", "eye_color": "black", "homeworld": "Kamino", "birth_year": "NA", "hair_color": "none", "skin_color": "grey" }, { "id": 70, "mass": "NA", "name": "Taun We", "gender": "female", "height": "213", "species": "Kaminoan", "eye_color": "black", "homeworld": "Kamino", "birth_year": "NA", "hair_color": "none", "skin_color": "grey" }, { "id": 71, "mass": "NA", "name": "Jocasta Nu", "gender": "female", "height": "167", "species": "Human", "eye_color": "blue", "homeworld": "Coruscant", "birth_year": "NA", "hair_color": "white", "skin_color": "fair" }, { "id": 72, "mass": "15", "name": "Ratts Tyerell", "gender": "male", "height": "79", "species": "Aleena", "eye_color": "NA", "homeworld": "Aleen Minor", "birth_year": "NA", "hair_color": "none", "skin_color": "grey, blue" }, { "id": 73, "mass": "NA", "name": "R4-P17", "gender": "female", "height": "96", "species": "NA", "eye_color": "red, blue", "homeworld": "NA", "birth_year": "NA", "hair_color": "none", "skin_color": "silver, red" }, { "id": 74, "mass": "48", "name": "Wat Tambor", "gender": "male", "height": "193", "species": "Skakoan", "eye_color": "NA", "homeworld": "Skako", "birth_year": "NA", "hair_color": "none", "skin_color": "green, grey" }, { "id": 75, "mass": "NA", "name": "San Hill", "gender": "male", "height": "191", "species": "Muun", "eye_color": "gold", "homeworld": "Muunilinst", "birth_year": "NA", "hair_color": "none", "skin_color": "grey" }, { "id": 76, "mass": "57", "name": "Shaak Ti", "gender": "female", "height": "178", "species": "Togruta", "eye_color": "black", "homeworld": "Shili", "birth_year": "NA", "hair_color": "none", "skin_color": "red, blue, white" }, { "id": 77, "mass": "159", "name": "Grievous", "gender": "male", "height": "216", "species": "Kaleesh", "eye_color": "green, yellow", "homeworld": "Kalee", "birth_year": "NA", "hair_color": "none", "skin_color": "brown, white" }, { "id": 78, "mass": "136", "name": "Tarfful", "gender": "male", "height": "234", "species": "Wookiee", "eye_color": "blue", "homeworld": "Kashyyyk", "birth_year": "NA", "hair_color": "brown", "skin_color": "brown" }, { "id": 79, "mass": "79", "name": "Raymus Antilles", "gender": "male", "height": "188", "species": "Human", "eye_color": "brown", "homeworld": "Alderaan", "birth_year": "NA", "hair_color": "brown", "skin_color": "light" }, { "id": 80, "mass": "48", "name": "Sly Moore", "gender": "female", "height": "178", "species": "NA", "eye_color": "white", "homeworld": "Umbara", "birth_year": "NA", "hair_color": "none", "skin_color": "pale" }, { "id": 81, "mass": "80", "name": "Tion Medon", "gender": "male", "height": "206", "species": "Pau'an", "eye_color": "black", "homeworld": "Utapau", "birth_year": "NA", "hair_color": "none", "skin_color": "grey" }, { "id": 82, "mass": "NA", "name": "Finn", "gender": "male", "height": "NA", "species": "Human", "eye_color": "dark", "homeworld": "NA", "birth_year": "NA", "hair_color": "black", "skin_color": "dark" }, { "id": 83, "mass": "NA", "name": "Rey", "gender": "female", "height": "NA", "species": "Human", "eye_color": "hazel", "homeworld": "NA", "birth_year": "NA", "hair_color": "brown", "skin_color": "light" }, { "id": 84, "mass": "NA", "name": "Poe Dameron", "gender": "male", "height": "NA", "species": "Human", "eye_color": "brown", "homeworld": "NA", "birth_year": "NA", "hair_color": "brown", "skin_color": "light" }, { "id": 85, "mass": "NA", "name": "BB8", "gender": "none", "height": "NA", "species": "Droid", "eye_color": "black", "homeworld": "NA", "birth_year": "NA", "hair_color": "none", "skin_color": "none" }, { "id": 86, "mass": "NA", "name": "Captain Phasma", "gender": "female", "height": "NA", "species": "NA", "eye_color": "NA", "homeworld": "NA", "birth_year": "NA", "hair_color": "NA", "skin_color": "NA" }, { "id": 87, "mass": "45", "name": "Padmé Amidala", "gender": "female", "height": "165", "species": "Human", "eye_color": "brown", "homeworld": "Naboo", "birth_year": "46BBY", "hair_color": "brown", "skin_color": "light" }, { "id": 88, "mass": null, "name": "", "gender": null, "height": null, "species": null, "eye_color": null, "homeworld": null, "birth_year": null, "hair_color": null, "skin_color": null } ]
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import com.fasterxml.jackson.databind.ObjectMapper;
URL url = new URL("https://api.api-ninjas.com/v1/cars?model=camry");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("accept", "application/json");
InputStream responseStream = connection.getInputStream();
ObjectMapper mapper = new ObjectMapper();
JsonNode root = mapper.readTree(responseStream);
System.out.println(root.path("fact").asText());
| import com.fasterxml.jackson.databind.ObjectMapper; package com.fasterxml.jackson.databind does not exist