public RequestBodyEntity body(JsonNode body) { this.body = body.toString(); return this; }
public RequestBodyEntity body(JsonNode body) { return body(body.toString()); }
public static JSONObject get(String urlCommand) { log.trace("get: %s", urlCommand); HttpResponse<JsonNode> jsonResponse = null; JSONObject response = null; try { jsonResponse = Unirest.get(urlBase + urlCommand) .header("accept", "application/json") .asJson(); } catch (UnirestException e) { log.error("get: %s", e.getMessage()); } String responseBody = "null"; if (SX.isNotNull(jsonResponse)) { responseBody = jsonResponse.getBody().toString(); response = SXJson.makeObject(responseBody); } log.trace("get: response: %s",jsonResponse.getBody().toString()); return response; }
public RequestBodyEntity body(JsonNode body) { this.body = body.toString(); return this; }
public RequestBodyEntity body(JsonNode body) { this.body = body.toString(); return this; }
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 RequestBodyEntity body(JsonNode body) { return body(body.toString()); }
public RequestBodyEntity body(JsonNode body) { return body(body.toString()); }
@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()); }
public static void main(String[] args) throws UnirestException { HttpResponse<JsonNode> jsonResponse = Unirest.get("http://httpbin.org/basic-auth/biezhi/123456") .basicAuth("biezhi", "123456").asJson(); System.out.println(jsonResponse.getBody().toString()); } }
public String deleteImposter(int port) { try { HttpResponse<JsonNode> response = Unirest.delete(baseUrl + "/imposters/" + port).asJson(); return response.getBody().toString(); } catch (UnirestException e) { return null; } }
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()); } }
public static void main(String[] args) throws UnirestException { HttpResponse<JsonNode> response = Unirest.get("http://httpbin.org/{method}") .routeParam("method", "get") .queryString("name", "biezhi") .asJson(); System.out.println(response.getBody().toString()); } }
public Imposter getImposter(int port) throws ParseException { try { HttpResponse<JsonNode> response = Unirest.get(baseUrl + "/imposters/" + port).asJson(); String responseJson = response.getBody().toString(); return ImposterParser.parse(responseJson); } catch (UnirestException e) { return null; } } }
@Test public void shouldDeleteAnImposter() throws UnirestException { String deleteResponse = "{ url: http://localhost:5757 }"; when(Unirest.delete(Client.DEFAULT_BASE_URL + "/imposters/5757")).thenReturn(requestWithBody); when(requestWithBody.asJson()).thenReturn(httpResponse); when(httpResponse.getBody()).thenReturn(value); when(value.toString()).thenReturn(deleteResponse); String response = client.deleteImposter(5757); assertThat(response).contains("5757").contains("http"); }
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()); } }
@Test public void shouldGetAnImposter() throws ParseException, UnirestException { int port = 5757; String jsonString = "test string"; Imposter expectedImposter = new ImposterBuilder().onPort(5757).build(); when(Unirest.get(Client.DEFAULT_BASE_URL + "/imposters/5757")).thenReturn(getRequest); when(getRequest.asJson()).thenReturn(httpResponse); when(httpResponse.getBody()).thenReturn(value); when(value.toString()).thenReturn(jsonString); when(ImposterParser.parse(jsonString)).thenReturn(expectedImposter); Imposter actualImposter = client.getImposter(port); assertThat(actualImposter.getPort()).isEqualTo(expectedImposter.getPort()); }
@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; } }