use of com.mashape.unirest.request.HttpRequestWithBody in project javalin by tipsy.
the class TestDefaultContentType method test_allows_overrides.
@Test
public void test_allows_overrides() throws Exception {
app.get("/test-override-encoding", ctx -> {
ctx.response().setCharacterEncoding("utf-8");
ctx.response().setContentType("text/html");
ctx.result("");
});
HttpResponse<String> response = new HttpRequestWithBody(HttpMethod.GET, origin + "/test-override-encoding").asString();
String charset = response.getHeaders().getFirst("Content-Type");
assertThat(charset, containsString("utf-8"));
assertThat(charset, containsString("text/html"));
}
use of com.mashape.unirest.request.HttpRequestWithBody in project javalin by tipsy.
the class TestDefaultContentType method test_sets_defaults.
@Test
public void test_sets_defaults() throws Exception {
app.get("/test-default-encoding", ctx -> ctx.result(""));
HttpResponse<String> response = new HttpRequestWithBody(HttpMethod.GET, origin + "/test-default-encoding").asString();
String charset = response.getHeaders().getFirst("Content-Type");
assertThat(charset, containsString("windows-1251"));
assertThat(charset, containsString("application/json"));
}
use of com.mashape.unirest.request.HttpRequestWithBody in project zeppelin by apache.
the class HttpBasedClient method search.
@Override
public ActionResponse search(String[] indices, String[] types, String query, int size) {
ActionResponse response = null;
if (!StringUtils.isEmpty(query)) {
// So, try to parse as a JSON => if there is an error, consider the query a Lucene one
try {
gson.fromJson(query, Map.class);
} catch (final JsonParseException e) {
// This is not a JSON (or maybe not well formatted...)
query = QUERY_STRING_TEMPLATE.replace("_Q_", query);
}
}
try {
final HttpRequestWithBody request = Unirest.post(getUrl(indices, types) + "/_search?size=" + size).header("Content-Type", "application/json");
if (StringUtils.isNoneEmpty(query)) {
request.header("Accept", "application/json").body(query);
}
if (StringUtils.isNotEmpty(username)) {
request.basicAuth(username, password);
}
final HttpResponse<JsonNode> result = request.asJson();
final JSONObject body = result.getBody() != null ? result.getBody().getObject() : null;
if (isSucceeded(result)) {
final long total = getTotal(result);
response = new ActionResponse().succeeded(true).totalHits(total);
if (containsAggs(result)) {
JSONObject aggregationsMap = body.getJSONObject("aggregations");
if (aggregationsMap == null) {
aggregationsMap = body.getJSONObject("aggs");
}
for (final String key : aggregationsMap.keySet()) {
final JSONObject aggResult = aggregationsMap.getJSONObject(key);
if (aggResult.has("buckets")) {
// Multi-bucket aggregations
final Iterator<Object> buckets = aggResult.getJSONArray("buckets").iterator();
while (buckets.hasNext()) {
response.addAggregation(new AggWrapper(AggregationType.MULTI_BUCKETS, buckets.next().toString()));
}
} else {
response.addAggregation(new AggWrapper(AggregationType.SIMPLE, aggregationsMap.toString()));
}
// Keep only one aggregation
break;
}
} else if (size > 0 && total > 0) {
final JSONArray hits = getFieldAsArray(body, "hits/hits");
final Iterator<Object> iter = hits.iterator();
while (iter.hasNext()) {
final JSONObject hit = (JSONObject) iter.next();
final Object data = hit.opt("_source") != null ? hit.opt("_source") : hit.opt("fields");
response.addHit(new HitWrapper(hit.getString("_index"), hit.getString("_type"), hit.getString("_id"), data.toString()));
}
}
} else {
throw new ActionException(body.get("error").toString());
}
} catch (final UnirestException e) {
throw new ActionException(e);
}
return response;
}
use of com.mashape.unirest.request.HttpRequestWithBody in project DiscLoader by R3alCl0ud.
the class APIRequest method createRequest.
public BaseRequest createRequest() throws IOException {
BaseRequest request = null;
switch(this.method) {
case Methods.GET:
request = Unirest.get(this.route);
break;
case Methods.POST:
request = Unirest.post(this.route);
if (multi) {
// try {
SendableMessage sdata = (SendableMessage) this.data;
File file = sdata.file;
Resource resource = sdata.resource;
byte[] bytes = new byte[0];
if (file != null)
bytes = DLUtil.readAllBytes(file);
if (resource != null)
bytes = DLUtil.readAllBytes(resource);
MultipartBody body = ((HttpRequestWithBody) request).fields(null);
String loc = "";
if (file != null)
loc = file.getName();
if (resource != null)
loc = resource.getFileName();
body.field("Content-type", "multipart/form-data").field("file", bytes, loc).field("payload_json", gson.toJson(sdata));
// } catch (IOException ex) {
// ex.printStackTrace();
// }
} else {
((HttpRequestWithBody) request).body(gson.toJson(data));
}
break;
case Methods.PATCH:
request = Unirest.patch(this.route);
((HttpRequestWithBody) request).body(gson.toJson(data));
break;
case Methods.DELETE:
request = Unirest.delete(this.route);
break;
case DLUtil.Methods.PUT:
request = Unirest.put(this.route);
((HttpRequestWithBody) request).body(gson.toJson(this.data));
break;
default:
request = Unirest.get(this.route);
break;
}
return request;
}
use of com.mashape.unirest.request.HttpRequestWithBody in project DiscLoader by R3alCl0ud.
the class Route method createRequest.
public BaseRequest createRequest(Request<T> request) {
BaseRequest base = null;
switch(method) {
case GET:
base = Unirest.get(endpoint);
break;
case POST:
base = Unirest.post(endpoint);
if (request.getData() instanceof SendableMessage) {
MultipartBody body = ((HttpRequestWithBody) base).fields(null);
body.field("Content-Type", "multipart/form-data");
SendableMessage sdata = (SendableMessage) request.getData();
if (sdata.file != null || sdata.resource != null) {
try {
File file = sdata.file;
Resource resource = sdata.resource;
byte[] bytes = new byte[0];
if (file != null)
bytes = DLUtil.readAllBytes(file);
if (resource != null && resource.getResourceAsStream() != null)
bytes = DLUtil.readAllBytes(resource);
String loc = "";
if (file != null)
loc = file.getName();
if (resource != null)
loc = resource.getFileName();
body.field("file", bytes, loc);
} catch (Exception ex) {
ex.printStackTrace();
}
}
try {
if (request.getData() instanceof JSONObject) {
body.field("payload_json", request.getData().toString());
} else if (request.getData() instanceof String) {
body.field("payload_json", request.getData());
} else {
body.field("payload_json", gson.toJson(request.getData()));
}
} catch (Exception ex) {
ex.printStackTrace();
}
} else {
base.getHttpRequest().header("Content-Type", "application/json");
if (request.getData() instanceof JSONObject) {
((HttpRequestWithBody) base).body(request.getData().toString());
} else if (request.getData() instanceof String) {
((HttpRequestWithBody) base).body(request.getData());
} else {
((HttpRequestWithBody) base).body(gson.toJson(request.getData()));
}
}
break;
case DELETE:
base = Unirest.delete(endpoint);
break;
case PATCH:
base = Unirest.patch(endpoint);
base.getHttpRequest().header("Content-Type", "application/json");
if (request.getData() instanceof JSONObject) {
((HttpRequestWithBody) base).body(request.getData().toString());
} else if (request.getData() instanceof String) {
((HttpRequestWithBody) base).body(request.getData().toString());
} else {
((HttpRequestWithBody) base).body(gson.toJson(request.getData()));
}
break;
case PUT:
base = Unirest.put(endpoint);
base.getHttpRequest().header("Content-Type", "application/json");
if (request.getData() instanceof JSONObject) {
((HttpRequestWithBody) base).body(request.getData().toString());
} else if (request.getData() instanceof String) {
((HttpRequestWithBody) base).body(request.getData());
} else {
((HttpRequestWithBody) base).body(gson.toJson(request.getData()));
}
break;
default:
base = Unirest.get(endpoint);
break;
}
// body.
HttpRequest httprequest = base.getHttpRequest();
if (auth && rest.loader.token != null)
httprequest.header("Authorization", rest.loader.token);
if (request.getOptions() != null && request.getOptions().getReason() != null) {
httprequest.header("X-Audit-Log-Reason", request.getOptions().getReason());
System.out.println("is this thing working?");
}
httprequest.header("user-agent", "DiscordBot (http://discloader.io, v0.2.0)");
httprequest.header("Accept-Encoding", "gzip");
return base;
}
Aggregations