Java Code Examples for com.mashape.unirest.http.Unirest#post()
The following examples show how to use
com.mashape.unirest.http.Unirest#post() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RestAction.java From cognitivej with Apache License 2.0 | 7 votes |
private HttpRequest buildUnirest(WorkingContext workingContext) { String url = String.format("%s%s", PROJECTOXFORD_AI, workingContext.getPathBuilt()); switch (workingContext.getHttpMethod()) { case GET: return Unirest.get(url); case PUT: return Unirest.put(url); case DELETE: return Unirest.delete(url); case PATCH: return Unirest.patch(url); } return Unirest.post(url); }
Example 2
Source File: AbstractRequest.java From alpaca-java with MIT License | 6 votes |
/** * Invoke post. * * @param abstractRequestBuilder the abstract request builder * * @return the http response */ public HttpResponse<InputStream> invokePost(AbstractRequestBuilder abstractRequestBuilder) { try { String url = abstractRequestBuilder.getURL(); LOGGER.debug("POST URL: " + url); HttpRequestWithBody request = Unirest.post(url); if (!headers.isEmpty()) { request.headers(headers); LOGGER.debug("POST Headers: " + headers); } String body = abstractRequestBuilder.getBody(); if (body != null) { request.body(body); LOGGER.debug("POST Body: " + body); } return request.asBinary(); } catch (UnirestException e) { LOGGER.error("UnirestException", e); } return null; }
Example 3
Source File: TelegramBot.java From JavaTelegramBot-API with GNU General Public License v3.0 | 6 votes |
/** * Use this method to get a new TelegramBot instance with the selected auth token * * @param authToken The bots auth token * * @return A new TelegramBot instance or null if something failed */ public static TelegramBot login(String authToken) { try { HttpRequestWithBody request = Unirest.post(API_URL + "bot" + authToken + "/getMe"); HttpResponse<String> response = request.asString(); JSONObject jsonResponse = Utils.processResponse(response); if (jsonResponse != null && Utils.checkResponseStatus(jsonResponse)) { JSONObject result = jsonResponse.getJSONObject("result"); return new TelegramBot(authToken, result.getInt("id"), result.getString("first_name"), result.getString("username")); } } catch (UnirestException e) { e.printStackTrace(); } return null; }
Example 4
Source File: PacketBuilder.java From discord.jar with The Unlicense | 5 votes |
public String makeRequest() { try { HttpRequestWithBody request; switch (type) { case DELETE: 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); } }
Example 5
Source File: CustomHttpClient.java From openvidu with Apache License 2.0 | 5 votes |
private JsonObject commonRest(HttpMethod method, String path, String body, int status) throws Exception { HttpResponse<?> jsonResponse = null; JsonObject json = null; path = openviduUrl + (path.startsWith("/") ? path : ("/" + path)); HttpRequest request = null; if (body != null && !body.isEmpty()) { switch (method) { case POST: request = Unirest.post(path); break; case PUT: request = Unirest.put(path); break; case PATCH: default: request = Unirest.patch(path); break; } ((HttpRequestWithBody) request).header("Content-Type", "application/json").body(body.replaceAll("'", "\"")); } else { switch (method) { case GET: request = Unirest.get(path); request.header("Content-Type", "application/x-www-form-urlencoded"); break; case POST: request = Unirest.post(path); break; case DELETE: request = Unirest.delete(path); request.header("Content-Type", "application/x-www-form-urlencoded"); break; case PUT: request = Unirest.put(path); default: break; } } request = request.header("Authorization", this.headerAuth); try { jsonResponse = request.asJson(); if (jsonResponse.getBody() != null) { jsonResponse.getBody(); json = JsonParser.parseString(((JsonNode) jsonResponse.getBody()).getObject().toString()) .getAsJsonObject(); } } catch (UnirestException e) { try { if (e.getCause().getCause().getCause() instanceof org.json.JSONException) { try { jsonResponse = request.asString(); } catch (UnirestException e1) { throw new Exception("Error sending request to " + path + ": " + e.getMessage()); } } else { throw new Exception("Error sending request to " + path + ": " + e.getMessage()); } } catch (NullPointerException e2) { throw new Exception("Error sending request to " + path + ": " + e.getMessage()); } } if (jsonResponse.getStatus() == 500) { log.error("Internal Server Error: {}", jsonResponse.getBody().toString()); } if (status != jsonResponse.getStatus()) { System.err.println(jsonResponse.getBody().toString()); throw new Exception(path + " expected to return status " + status + " but got " + jsonResponse.getStatus()); } return json; }
Example 6
Source File: UnirestDownloader.java From gecco with MIT License | 5 votes |
@Override public HttpResponse download(HttpRequest request) throws DownloaderException { if(log.isDebugEnabled()) { log.debug("downloading..." + request.getUrl()); } try { HttpHost proxy = Proxys.getProxy(); if(proxy != null) { Unirest.setProxy(proxy); } else { Unirest.setProxy(null); } request.addHeader("User-Agent", UserAgent.getUserAgent()); com.mashape.unirest.http.HttpResponse<String> response = null; if(request instanceof HttpPostRequest) { HttpPostRequest post = (HttpPostRequest)request; HttpRequestWithBody httpRequestWithBody = Unirest.post(post.getUrl()); httpRequestWithBody.headers(post.getHeaders()); httpRequestWithBody.fields(post.getFields()); response = httpRequestWithBody.asString(); } else { response = Unirest.get(request.getUrl()).headers(request.getHeaders()).asString(); } String contentType = response.getHeaders().getFirst("Content-Type"); HttpResponse resp = new HttpResponse(); resp.setStatus(response.getStatus()); resp.setRaw(response.getRawBody()); resp.setContent(response.getBody()); resp.setContentType(contentType); resp.setCharset(getCharset(request, contentType)); return resp; } catch (UnirestException e) { throw new DownloaderException(e); } }
Example 7
Source File: AppServicePacketTransport.java From swellrt with Apache License 2.0 | 5 votes |
private BaseRequest formRequest(Request packet) { HttpRequest httpRequest = null; String url = "https://" + serverAddress + apiAddress + packet.getUrl(); String method = packet.getMethod(); switch (method) { case "GET": httpRequest = Unirest.get(url); break; case "POST": httpRequest = Unirest.post(url); break; case "PUT": httpRequest = Unirest.put(url); break; } Map<String, String> headers = packet.getHeaders(); httpRequest = httpRequest.headers(headers); Map<String, Object> queryStrings = packet.getQueryStrings(); httpRequest = httpRequest.queryString(queryStrings); BaseRequest request = httpRequest; if(!method.equals("GET")) { JSONObject body = packet.getBody(); request = ((HttpRequestWithBody)request).body(body.toString()); } return request; }
Example 8
Source File: NearestNeighborsClient.java From deeplearning4j with Apache License 2.0 | 5 votes |
/** * Runs knn on the given index * with the given k (note that this is for data * already within the existing dataset not new data) * @param index the index of the * EXISTING ndarray * to run a search on * @param k the number of results * @return * @throws Exception */ public NearestNeighborsResults knn(int index, int k) throws Exception { NearestNeighborRequest request = new NearestNeighborRequest(); request.setInputIndex(index); request.setK(k); val req = Unirest.post(url + "/knn"); req.header("accept", "application/json") .header("Content-Type", "application/json").body(request); addAuthHeader(req); NearestNeighborsResults ret = req.asObject(NearestNeighborsResults.class).getBody(); return ret; }
Example 9
Source File: NearestNeighborsClient.java From deeplearning4j with Apache License 2.0 | 5 votes |
/** * Run a k nearest neighbors search * on a NEW data point * @param k the number of results * to retrieve * @param arr the array to run the search on. * Note that this must be a row vector * @return * @throws Exception */ public NearestNeighborsResults knnNew(int k, INDArray arr) throws Exception { Base64NDArrayBody base64NDArrayBody = Base64NDArrayBody.builder().k(k).ndarray(Nd4jBase64.base64String(arr)).build(); val req = Unirest.post(url + "/knnnew"); req.header("accept", "application/json") .header("Content-Type", "application/json").body(base64NDArrayBody); addAuthHeader(req); NearestNeighborsResults ret = req.asObject(NearestNeighborsResults.class).getBody(); return ret; }
Example 10
Source File: BaseTestCase.java From blade with Apache License 2.0 | 4 votes |
protected HttpRequestWithBody post(String path) { log.info("[POST] {}", (origin + path)); return Unirest.post(origin + path); }