Refine search
public static JSONObject post(String urlCommand, String body) { log.trace("post: %s body: %s", urlCommand, body); HttpResponse<JsonNode> jsonResponse = null; JSONObject response = null; try { jsonResponse = Unirest.post(urlBase + urlCommand) .header("accept", "application/json") .header("content-type", "application/json") .body(body) .asJson(); } catch (UnirestException e) { log.error("post(): %s", e.getMessage()); } String responseBody = "null"; if (SX.isNotNull(jsonResponse)) { responseBody = jsonResponse.getBody().toString(); response = SXJson.makeObject(responseBody); } log.trace("post: response: %s", responseBody); return response; }
public static void main(String[] args) throws UnirestException { HttpResponse<JsonNode> jsonResponse = Unirest.post("http://httpbin.org/post") .header("accept", "application/json") .queryString("apiKey", "123") .field("parameter", "value") .field("foo", "bar") .asJson(); System.out.println(jsonResponse.getBody().toString()); } }
private void whenTheCapacityIsRetrievedForExchange(String exchangeCode) throws UnirestException { String apiPath = GetCapacityForExchangeEndpoint.API_PATH.replace("{exchangeCode}", exchangeCode); String apiUrl = "http://localhost:8080" + apiPath; log("API Url", apiUrl); HttpResponse<String> httpResponse = Unirest.get(apiUrl).asString(); responseStatus = httpResponse.getStatus(); log("Response Status", responseStatus); responseContent = httpResponse.getBody(); log("Response Content", formatJson(responseContent)); }
request = Unirest.delete(url); break; case OPTIONS: request = Unirest.options(url); break; case PATCH: request = Unirest.patch(url); break; case POST: request = Unirest.post(url); break; case PUT: request = Unirest.put(url); break; case GET: return Unirest.get(url).header("authorization", "Bot " + api.getLoginTokens().getToken()).header("Content-Type", isForm ? "application/x-www-form-urlencoded" : (file ? "application/octet-stream" : "application/json; charset=utf-8")).asString().getBody(); default: throw new RuntimeException(); return request.header("authorization", "Bot " + api.getLoginTokens().getToken()).header("Content-Type", isForm ? "application/x-www-form-urlencoded" : (file ? "application/octet-stream" : "application/json; charset=utf-8")).body(data).asString().getBody(); } catch (UnirestException e) { throw new RuntimeException(e);
postRequest = Unirest.post(url + paramUrl); postRequest = Unirest.delete(url + paramUrl); postRequest = (contentType != null) ? postRequest.header("content-type", contentType) : postRequest; postRequest = (basicAuth != null) ? postRequest.basicAuth(basicAuth.username, basicAuth.password) : postRequest; String headName = (String) e.nextElement(); String value = headers.getProperty(headName); postRequest = postRequest.header(headName, value); track.fail("Postrequest:Error during call Api Rest op from URL::" + postRequest.getUrl() + "' \n" + response.getStatus() + response.getStatusText() + "\n" + ex.getMessage()); HttpRequest postRequestNoBody=null; if (operation == Op.POST) { postRequestNoBody = Unirest.post(url + paramUrl); postRequestNoBody = Unirest.delete(url + paramUrl); if (response != null && !expectedStatus.isExpected(response.getStatus())){ track.fail("Request Error during Api Call." + response.getStatus()+":" + response.getStatusText() +"\n"
public static void main(String[] args) throws UnirestException { Unirest.setObjectMapper(new ObjectMapper() { private com.fasterxml.jackson.databind.ObjectMapper jacksonObjectMapper = new com.fasterxml.jackson.databind.ObjectMapper(); HttpResponse<Book> bookResponse = Unirest.get("http://httpbin.org/books/1").asObject(Book.class); Book bookObject = bookResponse.getBody(); HttpResponse<Author> authorResponse = Unirest.get("http://httpbin.org/books/{id}/author") .routeParam("id", bookObject.getId()) .asObject(Author.class); Author authorObject = authorResponse.getBody(); HttpResponse<JsonNode> postResponse = Unirest.post("http://httpbin.org/authors/post") .header("accept", "application/json") .header("Content-Type", "application/json") .body(authorObject) .asJson(); System.out.println(postResponse.getBody().toString());
switch (method) { case GET: response = Unirest.get(url).queryString(parameters).asJson(); break; case POST: response = Unirest.post(url).fields(parameters).asJson(); break; case PUT: response = Unirest.put(url).fields(parameters).asJson(); break; case PATCH: response = Unirest.patch(url).fields(parameters).asJson(); break; case DELETE: response = Unirest.delete(url).fields(parameters).asJson(); break; default: if (response.getStatus() == 200) { JSONArray servers = response.getBody().getArray(); Set<InetSocketAddress> serverObjects = new HashSet<>(); for (int i = 0; i < servers.length(); i++) { Log.error(LOG, "The auto update URL returned " + response.getStatus());
/** * dumps a string to hastebin * * @param message the text to send * @return key how to find it */ private static String handleHastebin(String message) throws UnirestException { return Unirest.post("https://hastebin.com/documents").body(message).asJson().getBody().getObject().getString("key"); } }
public static HtmlAnalysisResult analyze(Map<String, String> config, String url) { try { String userAgent = config.getOrDefault(CONFIG_USER_AGENT, DEFAULT_USER_AGENT); HttpResponse<String> response = Unirest.get(url) .header("User-Agent", userAgent) .asString(); return analyze(config, url, response.getBody(), response.getStatus(), response.getHeaders()); } catch (UnirestException e) { throw new RuntimeException(e); } }
private void login() throws IOException { HttpResponse<JsonNode> loginResult; try { loginResult = Unirest.post(serverUrl + "v1/login").field("username", user).field("password", password).asJson(); } catch (UnirestException e) { throw new IOException(e); } if (loginResult.getStatus() == 401) throw new IOException("The username and password provided are incorrect."); if (loginResult.getStatus() != 200) throw new IOException("The login failed with a result of: " + loginResult.getStatus()); JSONObject data = loginResult.getBody().getObject().getJSONObject("data"); this.authToken = data.getString("authToken"); this.userId = data.getString("userId"); }
/** * Get public stats for the pihole installation * * @return * @throws UnirestException */ public PiHoleStats getStats() throws UnirestException { HttpResponse<String> response = Unirest .post(this.url + "api.php") .header("cache-control", "no-cache").asString(); logger.info("[PiHole] Query result: {} for url:[{}]", response.getBody(), this.url); return gson.fromJson(response.getBody(), PiHoleStats.class); }
/** * Kill all apps that are currently running. */ @Override public void killAllApps() { String marathonEndpoint = getServiceUrl().toString(); JSONObject appsResponse; try { appsResponse = Unirest.get(marathonEndpoint + APPS_ENDPOINT).header(HEADER_ACCEPT, APPLICATION_JSON).asJson().getBody().getObject(); if (appsResponse.length() == 0) { return; } } catch (UnirestException e) { throw new MinimesosException("Could not retrieve apps from Marathon at " + marathonEndpoint, e); } JSONArray apps = appsResponse.getJSONArray("apps"); for (int i = 0; i < apps.length(); i++) { JSONObject app = apps.getJSONObject(i); String appId = app.getString("id"); try { Unirest.delete(marathonEndpoint + APPS_ENDPOINT + appId).asJson(); } catch (UnirestException e) { //NOSONAR // failed to delete one app; continue with others LOGGER.error("Could not delete app " + appId + " at " + marathonEndpoint, e); } } }
protected HttpRequest get(String path) { log.info("[GET] {}", (origin + path)); return Unirest.get(origin + path); }