public MultipartBody field(String name, Collection<?> collection) { for (Object current : collection) { boolean isFile = current instanceof File; field(name, current, isFile, null); } return this; }
public MultipartBody field(String name, Collection<?> value) { MultipartBody body = new MultipartBody(this).field(name, value); this.body = body; return body; }
private JSONObject editMessageText(String chatId, Long messageId, String inlineMessageId, String text, ParseMode parseMode, boolean disableWebPagePreview, InlineReplyMarkup inlineReplyMarkup) { HttpResponse<String> response; JSONObject jsonResponse = null; try { MultipartBody requests = Unirest.post(getBotAPIUrl() + "editMessageText") .field("text", text, "application/json; charset=utf8;") .field("disable_web_page_preview", disableWebPagePreview); if(chatId != null) requests.field("chat_id", chatId, "application/json; charset=utf8;"); if(messageId != null) requests.field("message_id", messageId); if(inlineMessageId != null) requests.field("inline_message_id", inlineMessageId, "application/json; charset=utf8;"); if(parseMode != null) requests.field("parse_mode", parseMode.getModeName(), "application/json; charset=utf8;"); if(inlineReplyMarkup != null) requests.field("reply_markup", GSON.toJson(inlineReplyMarkup, InlineKeyboardMarkup.class), "application/json; charset=utf8;"); response = requests.asString(); jsonResponse = Utils.processResponse(response); } catch (UnirestException e) { e.printStackTrace(); } return jsonResponse; }
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()); } }
private static HttpResponse<JsonNode> requestJson(String path, Map<String, Object> params, File file) throws UnirestException { MultipartBody req = prepareRequest(path, params, file); // Execute and return response as JSON HttpResponse<JsonNode> resp = req.asJson(); // Record lastRequestPath = path; lastResponse = resp; lastResponseBodyClass = JsonNode.class; return resp; }
default HttpResponse<RAuth> auth(String username, String password) throws UnirestException, KevoreeRegistryException { HttpResponse<RAuth> res = Unirest.post(this.baseUrl() + "/oauth/token") .header("Content-Type", "application/x-www-form-urlencoded") .field("grant_type", "password") .field("username", username) .field("password", password) .field("scope", "read write") .field("client_id", config().getString("registry.oauth.client_id")) .field("client_secret", config().getString("registry.oauth.client_secret")) .asObject(RAuth.class); RAuth auth = res.getBody(); auth.setExpiresAt((System.currentTimeMillis() / 1000L) + auth.getExpiresIn()); if (res.getStatus() == 200) { try { config().save(); } catch (IOException e) { throw new KevoreeRegistryException("Unable to update config file", e); } } return res; }
public static void main(String[] args) throws UnirestException, ExecutionException, InterruptedException { Future<HttpResponse<JsonNode>> future = Unirest.post("http://httpbin.org/post") .header("accept", "application/json") .field("param1", "value1") .field("param2", "value2") .asJsonAsync(new Callback<JsonNode>() { @Override public void failed(UnirestException e) { System.out.println("The request has failed"); } @Override public void completed(HttpResponse<JsonNode> response) { int code = response.getStatus(); JsonNode body = response.getBody(); System.out.println("Status Code: " + code); System.out.println(body.toString()); } @Override public void cancelled() { System.out.println("The request has been cancelled"); } }); } }
/** * sends stats to discordlist.net */ public void sendStatsToDiscordlistNet() { if (!BotConfig.BOT_STATS_DISCORDLIST_NET || !allShardsReady()) { return; } int totGuilds = 0; for (DiscordBot shard : shards) { totGuilds += shard.getJda().getGuilds().size(); } Unirest.post("https://bots.discordlist.net/api.php") .field("token", BotConfig.BOT_STATS_DISCORDLIST_NET_TOKEN) .field("servers", totGuilds) .asStringAsync(); }
private static HttpResponse<String> requestString(String path, Map<String, Object> params, File file) throws UnirestException { MultipartBody req = prepareRequest(path, params, file); // Execute and return response as String HttpResponse<String> resp = req.asString(); // Record lastRequestPath = path; lastResponse = resp; lastResponseBodyClass = String.class; return resp; }
private JSONObject editMessageCaption(String chatId, Long messageId, String inlineMessageId, String caption, InlineReplyMarkup inlineReplyMarkup) { HttpResponse<String> response; JSONObject jsonResponse = null; try { MultipartBody requests = Unirest.post(getBotAPIUrl() + "editMessageCaption") .field("caption", caption, "application/json; charset=utf8;"); if(chatId != null) requests.field("chat_id", chatId, "application/json; charset=utf8;"); if(messageId != null) requests.field("message_id", messageId); if(inlineMessageId != null) requests.field("inline_message_id", inlineMessageId, "application/json; charset=utf8;"); if(inlineReplyMarkup != null) requests.field("reply_markup", GSON.toJson(inlineReplyMarkup, InlineKeyboardMarkup.class), "application/json; charset=utf8;"); response = requests.asString(); jsonResponse = Utils.processResponse(response); } catch (UnirestException e) { e.printStackTrace(); } return jsonResponse; }
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"); }
public ScriptRequestResponse retreiveRequestedRsyncScript(String datasetIdentifier, String scriptRequestUrl) throws DataCaptureModuleException { logger.fine("retreiveRequestedRsyncScript using dataset identifier + " + datasetIdentifier + " to " + scriptRequestUrl); try { //When the result is an error, html is returned from DCM instead of json. This causes the parser to blow up unhelpfully. //Stock unirest hasn't been updated in years, but in a fork this issue seems to be improved: https://github.com/OpenUnirest/unirest-java/issues/10 HttpResponse<JsonNode> scriptRequest = Unirest.post(scriptRequestUrl) .field("datasetIdentifier", datasetIdentifier) .asJson(); return DataCaptureModuleUtil.getScriptFromRequest(scriptRequest); } catch( UnirestException ex) { String error = "Error calling " + scriptRequestUrl + ". This likely indicates the DCM service returned an error page and not valid json. Unirest parsing error: " + ex; logger.info(error); throw new DataCaptureModuleException(error, ex); } }
default HttpResponse<RAuth> refresh() throws UnirestException, KevoreeRegistryException { HttpResponse<RAuth> res = Unirest.post(this.baseUrl() + "/oauth/token") .header("Content-Type", "application/x-www-form-urlencoded") .field("grant_type", "refresh_token") .field("scope", "read write") .field("refresh_token", config().getString("user.refresh_token")) .field("client_id", config().getString("registry.oauth.client_id")) .field("client_secret", config().getString("registry.oauth.client_secret")) .asObject(RAuth.class); RAuth auth = res.getBody(); auth.setExpiresAt((System.currentTimeMillis() / 1000L) + auth.getExpiresIn()); if (res.getStatus() == 200) { try { config().save(); } catch (IOException e) { throw new KevoreeRegistryException("Unable to update config file", e); } } return res; }
/** * Gets a List of ChatMember objects for all the people who are admin in the chat * * @return A List of ChatMember objects for all the people who are admin in the chat */ default List<ChatMember> getChatAdministrators() { try { MultipartBody request = Unirest.post(getBotInstance().getBotAPIUrl() + "getChatAdministrators") .field("chat_id", getId(), "application/json; charset=utf8;"); HttpResponse<String> response = request.asString(); JSONObject jsonResponse = processResponse(response); if (jsonResponse != null && Utils.checkResponseStatus(jsonResponse)) { JSONArray jsonArray = jsonResponse.getJSONArray("result"); List<ChatMember> chatAdmins = new ArrayList<>(); for(int i = 0; i < jsonArray.length(); ++i) { JSONObject jsonObject = jsonArray.getJSONObject(i); chatAdmins.add(createChatMember(jsonObject)); } return chatAdmins; } } catch (UnirestException e) { e.printStackTrace(); } return null; }
public MultipartBody field(String name, File file, String contentType) { return field(name, file, true, contentType); }
private JSONObject editMessageReplyMarkup(String chatId, Long messageId, String inlineMessageId, InlineReplyMarkup inlineReplyMarkup) { HttpResponse<String> response; JSONObject jsonResponse = null; try { MultipartBody requests = Unirest.post(getBotAPIUrl() + "editMessageReplyMarkup") .field("reply_markup", GSON.toJson(inlineReplyMarkup, InlineKeyboardMarkup.class), "application/json; charset=utf8;"); if(chatId != null) requests.field("chat_id", chatId, "application/json; charset=utf8;"); if(messageId != null) requests.field("message_id", messageId); if(inlineMessageId != null) requests.field("inline_message_id", inlineMessageId, "application/json; charset=utf8;"); response = requests.asString(); jsonResponse = Utils.processResponse(response); } catch (UnirestException e) { e.printStackTrace(); } return jsonResponse; }
public MultipartBody field(String name, File file, String contentType) { MultipartBody body = new MultipartBody(this).field(name, file, contentType); this.body = body; return body; }
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()); } }
/** * Gets the download link for this file * * @param telegramBot The TelegramBot instance that relates to this file * * @return The URL to download the file in String form */ default String getFileDownloadLink(TelegramBot telegramBot) { JSONObject jsonObject = null; try { jsonObject = Unirest.post(telegramBot.getBotAPIUrl() + "getFile") .field("file_id", getFileId(), "application/json; charset=utf8;") .asJson().getBody().getObject(); } catch (UnirestException e) { e.printStackTrace(); } if (jsonObject != null) { if (jsonObject.getBoolean("ok")) { return "https://api.telegram.org/file/bot" + telegramBot.getAuthToken() + "/" + jsonObject.getJSONObject("result").getString("file_path"); } } return null; }
response = uploadRequest.asString(); } catch (UnirestException e) { logger.error("Exception while connecting to Microcks backend", e);