本文整理了Java中com.mashape.unirest.http.Unirest
类的一些代码示例,展示了Unirest
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Unirest
类的具体详情如下:
包路径:com.mashape.unirest.http.Unirest
类名称:Unirest
暂无
代码示例来源:origin: lets-blade/blade
protected String getBodyString(String path) throws Exception {
log.info("[GET] {}", (origin + path));
return Unirest.get(origin + path).asString().getBody();
}
代码示例来源:origin: lets-blade/blade
protected String postBodyString(String path) throws Exception {
log.info("[POST] {}", (origin + path));
return Unirest.post(origin + path).asString().getBody();
}
代码示例来源:origin: lets-blade/blade
protected String deleteBodyString(String path) throws Exception {
log.info("[DELETE] {}", (origin + path));
return Unirest.delete(origin + path).asString().getBody();
}
代码示例来源:origin: RaiMan/SikuliX2
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;
}
代码示例来源:origin: biezhi/java-library-examples
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());
}
}
代码示例来源:origin: mattia-battiston/clean-architecture-example
private void whenTheCapacityIsRetrievedForExchange(String exchangeCode) throws UnirestException {
String apiPath = GetCapacityForExchangeEndpoint.API_PATH.replace("{exchangeCode}", exchangeCode);
String apiUrl = "http://localhost:8080" + apiPath;
log("API Url", apiUrl);
HttpResponse<String> httpResponse = Unirest.get(apiUrl).asString();
responseStatus = httpResponse.getStatus();
log("Response Status", responseStatus);
responseContent = httpResponse.getBody();
log("Response Content", formatJson(responseContent));
}
代码示例来源:origin: discord-java/discord.jar
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);
代码示例来源:origin: gradle.plugin.GoBqa/gradle-plugin
postRequest = Unirest.post(url + paramUrl);
postRequest = Unirest.delete(url + paramUrl);
postRequest = (contentType != null) ? postRequest.header("content-type", contentType) : postRequest;
postRequest = (basicAuth != null) ? postRequest.basicAuth(basicAuth.username, basicAuth.password) : postRequest;
String headName = (String) e.nextElement();
String value = headers.getProperty(headName);
postRequest = postRequest.header(headName, value);
track.fail("Postrequest:Error during call Api Rest op from URL::" + postRequest.getUrl() + "' \n"
+ response.getStatus() + response.getStatusText() + "\n"
+ ex.getMessage());
HttpRequest postRequestNoBody=null;
if (operation == Op.POST) {
postRequestNoBody = Unirest.post(url + paramUrl);
postRequestNoBody = Unirest.delete(url + paramUrl);
if (response != null && !expectedStatus.isExpected(response.getStatus())){
track.fail("Request Error during Api Call."
+ response.getStatus()+":" + response.getStatusText() +"\n"
代码示例来源:origin: streampipes/streampipes-ce
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();
}
代码示例来源:origin: biezhi/java-library-examples
public static void main(String[] args) throws UnirestException {
Unirest.setObjectMapper(new ObjectMapper() {
private com.fasterxml.jackson.databind.ObjectMapper jacksonObjectMapper
= new com.fasterxml.jackson.databind.ObjectMapper();
HttpResponse<Book> bookResponse = Unirest.get("http://httpbin.org/books/1").asObject(Book.class);
Book bookObject = bookResponse.getBody();
HttpResponse<Author> authorResponse = Unirest.get("http://httpbin.org/books/{id}/author")
.routeParam("id", bookObject.getId())
.asObject(Author.class);
Author authorObject = authorResponse.getBody();
HttpResponse<JsonNode> postResponse = Unirest.post("http://httpbin.org/authors/post")
.header("accept", "application/json")
.header("Content-Type", "application/json")
.body(authorObject)
.asJson();
System.out.println(postResponse.getBody().toString());
代码示例来源:origin: Kong/Astronode-Broadcaster
switch (method) {
case GET:
response = Unirest.get(url).queryString(parameters).asJson();
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:
if (response.getStatus() == 200) {
JSONArray servers = response.getBody().getArray();
Set<InetSocketAddress> serverObjects = new HashSet<>();
for (int i = 0; i < servers.length(); i++) {
Log.error(LOG, "The auto update URL returned " + response.getStatus());
代码示例来源:origin: Kaaz/DiscordBot
/**
* dumps a string to hastebin
*
* @param message the text to send
* @return key how to find it
*/
private static String handleHastebin(String message) throws UnirestException {
return Unirest.post("https://hastebin.com/documents").body(message).asJson().getBody().getObject().getString("key");
}
}
代码示例来源:origin: lt.tokenmill.crawling/page-analyzer
public static HtmlAnalysisResult analyze(Map<String, String> config, String url) {
try {
String userAgent = config.getOrDefault(CONFIG_USER_AGENT, DEFAULT_USER_AGENT);
HttpResponse<String> response = Unirest.get(url)
.header("User-Agent", userAgent)
.asString();
return analyze(config, url, response.getBody(), response.getStatus(), response.getHeaders());
} catch (UnirestException e) {
throw new RuntimeException(e);
}
}
代码示例来源:origin: thejamesthomas/javabank
public int createImposter(Imposter imposter) {
try {
HttpResponse<JsonNode> response = Unirest.post(baseUrl + "/imposters").body(imposter.toString()).asJson();
return response.getStatus();
} catch (UnirestException e) {
return 500;
}
}
代码示例来源:origin: baloise/rocket-chat-rest-client
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");
}
代码示例来源:origin: lets-blade/blade
@Test
public void testListenAddress() throws Exception {
Blade blade = Blade.of();
blade.listen("localhost", 9002).start().await();
try {
int code = Unirest.get("http://localhost:9002/").asString().getStatus();
assertEquals(404, code);
} finally {
blade.stop();
}
}
代码示例来源:origin: Kaaz/DiscordBot
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());
}
代码示例来源:origin: lamarios/Homedash2
/**
* 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);
}
代码示例来源:origin: ContainerSolutions/minimesos
/**
* Kill all apps that are currently running.
*/
@Override
public void killAllApps() {
String marathonEndpoint = getServiceUrl().toString();
JSONObject appsResponse;
try {
appsResponse = Unirest.get(marathonEndpoint + APPS_ENDPOINT).header(HEADER_ACCEPT, APPLICATION_JSON).asJson().getBody().getObject();
if (appsResponse.length() == 0) {
return;
}
} catch (UnirestException e) {
throw new MinimesosException("Could not retrieve apps from Marathon at " + marathonEndpoint, e);
}
JSONArray apps = appsResponse.getJSONArray("apps");
for (int i = 0; i < apps.length(); i++) {
JSONObject app = apps.getJSONObject(i);
String appId = app.getString("id");
try {
Unirest.delete(marathonEndpoint + APPS_ENDPOINT + appId).asJson();
} catch (UnirestException e) { //NOSONAR
// failed to delete one app; continue with others
LOGGER.error("Could not delete app " + appId + " at " + marathonEndpoint, e);
}
}
}
代码示例来源:origin: lets-blade/blade
protected HttpRequest get(String path) {
log.info("[GET] {}", (origin + path));
return Unirest.get(origin + path);
}
内容来源于网络,如有侵权,请联系作者删除!