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; }
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); } }
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 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 Image uploadImage(File file) throws Exception { // {"data":{"bandwidth":0,"nsfw":null,"is_ad":false,"link":"http://i.imgur.com/4a2U4HI.png","description":null,"section":null,"title":null,"type":"image/png","tags":[],"deletehash":"g5KtSKSSXT2l54Q","datetime":1490023631,"account_id":0,"size":258,"width":35,"account_url":null,"name":"","animated":false,"id":"4a2U4HI","in_gallery":false,"vote":null,"favorite":false,"views":0,"height":35},"success":true,"status":200} HttpResponse<JsonNode> response = Unirest .post("https://api.imgur.com/3/image") .header("accept", "application/json") .header("Authorization", "Client-ID " + clientId) .field("image", file) .field("name", file.getName()) .field("title", file.getName()) .asJson(); if (!response.getBody().getObject().getBoolean("success")) { Logger.debug(response.getBody().toString()); throw new Exception(response.getBody().getObject().getJSONObject("data").getString("error")); } Image image = new Image(); image.id = response.getBody().getObject().getJSONObject("data").getString("id"); image.deleteHash = response.getBody().getObject().getJSONObject("data").getString("deletehash"); return image; }
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()); } }
public Album createAlbum(String description, Image... images) throws Exception { String[] imageDeleteHashes = new String[images.length]; for (int i = 0; i < images.length; i++) { imageDeleteHashes[i] = images[i].deleteHash; } // {"data":{"id":"ZrUmj","deletehash":"m2BpMxDliK0FEgk"},"success":true,"status":200} HttpResponse<JsonNode> response = Unirest .post("https://api.imgur.com/3/album") .header("accept", "application/json") .header("Authorization", "Client-ID " + clientId) .field("description", description) .field("privacy", "hidden") .field("deletehashes", String.join(",", imageDeleteHashes)) .asJson(); if (!response.getBody().getObject().getBoolean("success")) { Logger.debug(response.getBody().toString()); throw new Exception(response.getBody().getObject().getJSONObject("data").getString("error")); } Album album = new Album(); album.id = response.getBody().getObject().getJSONObject("data").getString("id"); album.deleteHash = response.getBody().getObject().getJSONObject("data").getString("deletehash"); return album; }
/** * 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; }
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:
@Override public Message sendFile(File file) { JDAImpl api = (JDAImpl) getJDA(); try { HttpResponse<JsonNode> response = Unirest.post("https://discordapp.com/api/channels/" + getId() + "/messages") .header("authorization", getJDA().getAuthToken()) .header("user-agent", JDAInfo.GITHUB + " " + JDAInfo.VERSION) .field("file", file) .asJson(); JSONObject messageJson = new JSONObject(response.getBody().toString()); return new EntityBuilder(api).createMessage(messageJson); } catch (UnirestException e) { e.printStackTrace(); return null; } }
@Override public Message sendFile(File file) { JDAImpl api = (JDAImpl) getJDA(); try { HttpResponse<JsonNode> response = Unirest.post("https://discordapp.com/api/channels/" + getId() + "/messages") .header("authorization", getJDA().getAuthToken()) .header("user-agent", JDAInfo.GITHUB + " " + JDAInfo.VERSION) .field("file", file) .asJson(); JSONObject messageJson = new JSONObject(response.getBody().toString()); return new EntityBuilder(api).createMessage(messageJson); } catch (UnirestException e) { e.printStackTrace(); return null; } }
@Override public void open() throws TexeraException { if (cursor == OPENED) { return; } try { String asterixAddress = "http://" + predicate.getHost() + ":" + predicate.getPort() + "/query/service"; String asterixQuery = generateAsterixQuery(predicate); HttpResponse<JsonNode> jsonResponse = Unirest.post(asterixAddress) .queryString("statement", asterixQuery) .field("mode", "immediate") .asJson(); // if status is 200 OK, store the results if (jsonResponse.getStatus() == 200) { this.resultJsonArray = jsonResponse.getBody().getObject().getJSONArray("results"); } else { throw new DataflowException("Send query to asterix failed: " + "error status: " + jsonResponse.getStatusText() + ", " + "error body: " + jsonResponse.getBody().toString()); } cursor = OPENED; } catch (UnirestException e) { throw new DataflowException(e); } }
.field("message", commitMessage) .field("branch", resource.getBranch()) .asJson();