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; }
private static MultipartBody prepareRequest(String path, Map<String, Object> params, File file) { Locale defaultLocale = Locale.getDefault(); // Set up a JSON request MultipartBody req = Unirest.post(BASE_URL + path) .header(JSON_TYPE_KEY, JSON_TYPE) .header(USER_AGENT_KEY, USER_AGENT) .header(ACCEPT_LANGUAGE_KEY, acceptLanguageString()) .header(ACCEPT_LOCALE_KEY, defaultLocale.toString()) .fields(params); // Add file if there is one if (file != null) { return req.field(DATA_KEY, file); } return req; }
public void create() { JSONObject jsonOut = new JSONObject(); jsonOut.put("user", user) .put("key", key) .put("nick", session); RequestBodyEntity post = Unirest.post("https://cleverbot.io/1.0/create").header("Content-Type", "application/json") .body(jsonOut.toString()); }
public String ask(String query) throws UnirestException { JSONObject jsonOut = new JSONObject(); jsonOut.put("user", user) .put("key", key) .put("nick", session) .put("text", query); RequestBodyEntity post = Unirest.post("https://cleverbot.io/1.0/ask").header("Content-Type", "application/json") .body(jsonOut.toString()); JSONObject json = post.asJson().getBody().getObject(); return json.getString("response"); } }
public static void sendWebhook(Webhook webhook, WebhookContent content) { JSONObject json = new JSONObject(); json.put("content", content.message); json.put("username", content.name.replaceAll("&([0-9a-fA-FlLkKrR])", "")); json.put("avatar_url", content.avatarUrl); try { Unirest.post(webhook.getUrl()) .header("Content-Type", "application/json") .body(json) .asJsonAsync(); } catch (Exception e) { MagiBridge.getLogger().error("Error delivering Webhook request: "); e.printStackTrace(); } }
private void buildBody(HttpRequestWithBody builtRequest) throws IOException { if (workingContext().getPayload().containsKey(IMAGE_INPUT_STREAM_KEY)) { builtRequest.header("content-type", "application/octet-stream").body(IOUtils.toByteArray((InputStream) workingContext().getPayload().get(IMAGE_INPUT_STREAM_KEY))); } else { builtRequest.header("content-type", "application/json").body(new JSONObject(workingContext().getPayload())); } }
public void sendStatsToDiscordbotsOrg() { if (BotConfig.BOT_TOKEN_DISCORDBOTS_ORG.length() < 10) { return; } JSONObject data = new JSONObject(); data.put("server_count", getJda().getGuilds().size()); if (totShards > 1) { data.put("shard_id", shardId); data.put("shard_count", totShards); } Unirest.post("https://discordbots.org/api/bots/" + getJda().getSelfUser().getId() + "/stats") .header("Authorization", BotConfig.BOT_TOKEN_DISCORDBOTS_ORG) .header("Content-Type", "application/json") .body(data.toString()) .asJsonAsync(); }
/** * 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); }
@Override public SMSResponse sendSingleSMS(SMS sms) { ClickatellSMS clickatellSMS = new ClickatellSMS(); clickatellSMS.addTo(parseMobileNumber(sms.getTo().get(0))); clickatellSMS.setText(clickatellSMS.getText()); RequestBodyEntity requestWithBody = Unirest.post(settings.getUrl()) .header("Authorization", settings.getGrantType() + " " + settings.getToken()) .header("Content-Type", "application/json") .header("Accept", "application/json") .header("X-Version", settings.getVersion()) .body(clickatellSMS); return getSMSResponse(requestWithBody); }
public static void main(String[] args) throws UnirestException { HttpResponse<JsonNode> jsonResponse = Unirest.post("http://httpbin.org/post") .header("accept", "application/json") .field("parameter", "value") .field("file", new File("/tmp/file")) .asJson(); System.out.println(jsonResponse.getBody().toString()); } }
public static void main(String[] args) throws UnirestException { HttpResponse<JsonNode> jsonResponse = Unirest.post("http://httpbin.org/post") .header("accept", "application/json") .body("{\"parameter\":\"value\", \"foo\":\"bar\"}") .asJson(); System.out.println(jsonResponse.getBody().toString()); } }
private static int registerServiceHttpClient(String body) throws UnirestException { HttpResponse<JsonNode> jsonResponse = Unirest.put(consulURL().toString() + "/" + CONSUL_URL_REGISTER_SERVICE) .header("accept", "application/json") .body(body) .asJson(); return jsonResponse.getStatus(); }
default HttpResponse<RTypeDefinition> createTdef(String namespace, RTypeDefinition tdef) throws UnirestException, KevoreeRegistryException { // be sure user is logged-in / refresh token if necessary / throws otherwise this.ensureLogin(); // POST tdef return Unirest.post(this.baseUrl() + "/api/namespaces/{namespace}/tdefs") .header("Authorization", "Bearer " + config().getString("user.access_token")) .header("Content-Type", "application/json") .routeParam("namespace", namespace) .body(tdef) .asObject(RTypeDefinition.class); }
private BaseRequest executePost(String baseUrl, Object urlMap, Object bodyMap) { return Unirest.post(baseUrl + urlTemplate.execute(urlMap)).header("Content-type", "application/json").body(bodyTemplate.execute(bodyMap)); }
private BaseRequest executePut(String baseUrl, Object urlMap, Object bodyMap) { return Unirest.put(baseUrl + urlTemplate.execute(urlMap)).header("Content-type", "application/json").body(bodyTemplate.execute(bodyMap)); }
@Override public void addMovie(MovieRequest request) throws UnsupportedEncodingException, UnirestException { Gson gson = new Gson(); RadarrMovieRequest radarrMovieRequest = gson.fromJson(request.getMovie().rawJson, RadarrMovieRequest.class); radarrMovieRequest.setMonitored(true); radarrMovieRequest.setQualityProfileId(request.getQuality().getId()); radarrMovieRequest.setRootFolderPath(request.getFolder().getName()); System.out.println(request); Unirest.post(String.format(url, API_MOVIE_LIST)) .header("Content-Type", "application/json") .body(gson.toJson(radarrMovieRequest)) .asString().getBody(); }
default HttpResponse<RDeployUnit> createDu(String namespace, String name, long version, RDeployUnit du) throws UnirestException, KevoreeRegistryException { // be sure user is logged-in / refresh token if necessary / throws otherwise this.ensureLogin(); // POST du return Unirest.post(this.baseUrl() + "/api/namespaces/{namespace}/tdefs/{name}/{version}/dus") .header("Authorization", "Bearer " + config().getString("user.access_token")) .header("Content-Type", "application/json") .routeParam("namespace", namespace) .routeParam("name", name) .routeParam("version", String.valueOf(version)) .body(du) .asObject(RDeployUnit.class); }
static public JsonNode post(String url, JSONObject json) { HttpResponse<JsonNode> jsonResponse = null; try { jsonResponse = Unirest.post(url).header("content-type", "application/json").body(json).asJson(); } catch (UnirestException e) { unirestCrash(e); } return jsonResponse.getBody(); }
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()); } }
default HttpResponse<JsonNode> deleteTdef(String namespace, String name, long version) throws UnirestException, KevoreeRegistryException { // be sure user is logged-in / refresh token if necessary / throws otherwise this.ensureLogin(); // DELETE tdef return Unirest.delete(this.baseUrl() + "/api/namespaces/{namespace}/tdefs/{name}/{version}") .header("Authorization", "Bearer " + config().getString("user.access_token")) .routeParam("namespace", namespace) .routeParam("name", name) .routeParam("version", String.valueOf(version)) .asJson(); } }