本文整理汇总了Java中com.mashape.unirest.request.HttpRequestWithBody类的典型用法代码示例。如果您正苦于以下问题:Java HttpRequestWithBody类的具体用法?Java HttpRequestWithBody怎么用?Java HttpRequestWithBody使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
HttpRequestWithBody类属于com.mashape.unirest.request包,在下文中一共展示了HttpRequestWithBody类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: fetchSchemaFromRemote
import com.mashape.unirest.request.HttpRequestWithBody; //导入依赖的package包/类
public static String fetchSchemaFromRemote(String url, String basicAuthUsername, String basicAuthPassword) {
Map<String, String> bodyMap = new HashMap<>();
bodyMap.put("query", introspectionQuery());
bodyMap.put("variables", null);
HttpRequestWithBody requestWithBody = Unirest.post(url)
.header("Content-Type", "application/json")
.header("accept", "application/json");
// basic auth
if (basicAuthUsername != null && basicAuthPassword != null) {
requestWithBody.basicAuth(basicAuthUsername, basicAuthPassword);
}
// body
RequestBodyEntity requestBodyEntity = requestWithBody.body(bodyMap);
HttpResponse<JsonNode> jsonNodeHttpResponse;
try {
jsonNodeHttpResponse = requestBodyEntity.asJson();
} catch (UnirestException e) {
throw new RuntimeException(e);
}
return Util.convertStreamToString(jsonNodeHttpResponse.getRawBody(), "UTF-8");
}
示例2: post
import com.mashape.unirest.request.HttpRequestWithBody; //导入依赖的package包/类
public Response<Object> post(String url,
Map<String, String> routeParamsMap,
Map<String, String> queryStringMap,
Map<String, String> headersMap,
Object bodyObject) throws HttpClientException {
HttpRequestWithBody request = Unirest.post(url);
return executeHttpRequestWithBody(routeParamsMap, queryStringMap, headersMap, bodyObject, request);
}
示例3: notifyEvent
import com.mashape.unirest.request.HttpRequestWithBody; //导入依赖的package包/类
public EventResult notifyEvent(Incident incident) throws NotifyEventException {
try {
HttpRequestWithBody request = Unirest.post(eventApi)
.header("Accept", "application/json");
request.body(incident);
HttpResponse<JsonNode> jsonResponse = request.asJson();
log.debug(IOUtils.toString(jsonResponse.getRawBody()));
switch(jsonResponse.getStatus()) {
case HttpStatus.SC_OK:
case HttpStatus.SC_CREATED:
case HttpStatus.SC_ACCEPTED:
return EventResult.successEvent(JsonUtils.getPropertyValue(jsonResponse, "status"), JsonUtils.getPropertyValue(jsonResponse, "message"), JsonUtils.getPropertyValue(jsonResponse, "dedup_key"));
case HttpStatus.SC_BAD_REQUEST:
return EventResult.errorEvent(JsonUtils.getPropertyValue(jsonResponse, "status"), JsonUtils.getPropertyValue(jsonResponse, "message"), JsonUtils.getArrayValue(jsonResponse, "errors"));
default:
return EventResult.errorEvent(String.valueOf(jsonResponse.getStatus()), "", IOUtils.toString(jsonResponse.getRawBody()));
}
} catch (UnirestException | IOException e) {
throw new NotifyEventException(e);
}
}
示例4: updatePublishRepo
import com.mashape.unirest.request.HttpRequestWithBody; //导入依赖的package包/类
public void updatePublishRepo(String prefix, String distribution) throws AptlyRestException
{
try {
HttpRequestWithBody req = Unirest.put(mSite.getUrl() + "/api/publish/"
+ prefix + "/" + distribution);
req = req.header("Content-Type", "application/json");
JSONObject options = new JSONObject();
options.put("ForceOverwrite", true);
options.put("Signing",buildSigningJson());
req.body(options.toString());
JSONObject res = sendRequest(req);
}
catch (AptlyRestException ex) {
mLogger.printf("Failed to publish repo: " + ex.toString());
throw ex;
}
}
示例5: request
import com.mashape.unirest.request.HttpRequestWithBody; //导入依赖的package包/类
/**
* Perform a request with body to the API
*
* @param httpMethod
* @param url path of request
* @param body content to be sent
* @param opts
* @return the request response
*/
public JsonNode request(HttpMethod httpMethod, String url, JsonNode body, RequestOptions opts) {
if (opts == null) {
opts = RequestOptions.defaults();
}
prepareRequest();
HttpRequestWithBody req = createRequest(httpMethod, url, opts);
if (body != null) {
req.body(body);
}
return tryRequest(req, opts);
}
示例6: get
import com.mashape.unirest.request.HttpRequestWithBody; //导入依赖的package包/类
public String get(String key) throws Exception {
HttpRequest request = new HttpRequestWithBody(
HttpMethod.GET,
makeConsulUrl(key) + "?raw"
).getHttpRequest();
authorizeHttpRequest(request);
HttpResponse<String> response;
try {
response = HttpClientHelper.request(request, String.class);
} catch (Exception exception) {
throw new ConsulException("Consul request failed", exception);
}
if (response.getStatus() == 404) {
return null;
}
String encrypted = response.getBody();
return encryption.decrypt(encrypted);
}
示例7: doWork
import com.mashape.unirest.request.HttpRequestWithBody; //导入依赖的package包/类
private T doWork() {
try {
setupErrorHandlers();
WorkingContext workingContext = workingContext();
HttpRequest builtRequest = buildUnirest(workingContext)
.queryString(workingContext.getQueryParams())
.headers(workingContext.getHeaders()).header("Ocp-Apim-Subscription-Key", cognitiveContext.subscriptionKey);
if (!workingContext.getHttpMethod().equals(HttpMethod.GET) && workingContext().getPayload().size() > 0) {
buildBody((HttpRequestWithBody) builtRequest);
}
HttpResponse response;
if (typedResponse() == InputStream.class)
response = builtRequest.asBinary();
else
response = builtRequest.asString();
checkForError(response);
return postProcess(typeResponse(response.getBody()));
} catch (UnirestException | IOException e) {
throw new CognitiveException(e);
}
}
示例8: createPlaylist
import com.mashape.unirest.request.HttpRequestWithBody; //导入依赖的package包/类
private static JSONObject createPlaylist(String spotifyId, String title, String authHeader) {
HttpRequestWithBody http =
Unirest.post("https://api.spotify.com/v1/users/" + spotifyId + "/playlists");
try {
HttpResponse<JsonNode> httpResponse = http
.header("Content-Type", "application/json")
.header("user-agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36")
.header("Authorization", authHeader)
.body("{\"name\":\"" + title + "\", \"public\":true}")
.asJson();
JsonNode response = httpResponse.getBody();
return response.getObject();
} catch (Exception e) {
e.printStackTrace();
throw new WrapperException();
}
}
示例9: createCollaborativePlaylist
import com.mashape.unirest.request.HttpRequestWithBody; //导入依赖的package包/类
public static JSONObject createCollaborativePlaylist(String spotifyId, String title, String authHeader) {
HttpRequestWithBody http =
Unirest.post("https://api.spotify.com/v1/users/" + spotifyId + "/playlists");
try {
HttpResponse<JsonNode> httpResponse = http
.header("Content-Type", "application/json")
.header("user-agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36")
.header("Authorization", authHeader)
.body("{\"name\":\"" + title + "\", \"public\":false, \"collaborative\":true}")
.asJson();
JsonNode response = httpResponse.getBody();
return response.getObject();
} catch (Exception e) {
e.printStackTrace();
throw new WrapperException();
}
}
示例10: tokenCode
import com.mashape.unirest.request.HttpRequestWithBody; //导入依赖的package包/类
public static TokenResponse tokenCode(String code) {
HttpRequestWithBody http = Unirest.post(TOKEN_ROOT);
String authorizationHeader = getAuthorizationHeader();
String redirectUrl = getRedirectUrl();
try {
HttpResponse<String> httpResponse = http
.queryString("code", code)
.queryString("grant_type", "authorization_code")
.queryString("redirect_uri", redirectUrl)
.header("Content-Type", "application/x-www-form-urlencoded")
.header("user-agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36")
.header("Authorization", authorizationHeader)
.asString();
return gson.fromJson(httpResponse.getBody(), TokenResponse.class);
} catch (UnirestException e) {
e.printStackTrace();
throw new AuthException("Unirest error");
}
}
示例11: tokenRefresh
import com.mashape.unirest.request.HttpRequestWithBody; //导入依赖的package包/类
public static TokenResponse tokenRefresh(String refreshToken) {
HttpRequestWithBody http = Unirest.post(TOKEN_ROOT);
String authorizationHeader = getAuthorizationHeader();
try {
HttpResponse<String> httpResponse = http
.queryString("refresh_token", refreshToken)
.queryString("grant_type", "authorization_code")
.header("Content-Type", "application/x-www-form-urlencoded")
.header("user-agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36")
.header("Authorization", authorizationHeader)
.asString();
return gson.fromJson(httpResponse.getBody(), TokenResponse.class);
} catch (UnirestException e) {
e.printStackTrace();
throw new AuthException("Unirest error");
}
}
示例12: post
import com.mashape.unirest.request.HttpRequestWithBody; //导入依赖的package包/类
/**
* @param cmd
* REST post type
* @param body
* REST Payload
* @param params
* to fill request with
* @param objs
* objects to replace in url
* @return Request with prepared parameters values
* @throws Exception
*/
public BaseRequest post(HbPost cmd, Object body, Params params,
String... objs)
throws Exception {
if (body == null) {
return post(cmd, params, objs);
} else {
HttpRequestWithBody postReq = post(apiLink + cmd.getCmd(objs));
if (params != null) {
postReq = postReq.queryString(params.getAll());
}
RequestBodyEntity entity;
if (body instanceof JsonNode) {
entity = postReq.body((JsonNode) body);
}
else if (body instanceof String) {
entity = postReq.body((String) body);
}
else {
entity = postReq.body(body);
}
return entity;
}
}
示例13: put
import com.mashape.unirest.request.HttpRequestWithBody; //导入依赖的package包/类
/**
* Makes PUT RESful API method based on {@link HbPost}
*
* @param cmd
* API command of type {@link HbPut}
* @param params
* REST parameters
* @param objs
* objects to replace in url
* @return {@link RequestBodyEntity}
* @throws Exception
*/
public BaseRequest put(HbPut cmd, Object body, Params params,
String... objs)
throws Exception {
HttpRequestWithBody putReq = put(apiLink + cmd.getCmd(objs));
putReq = putReq.queryString(params.getAll());
if (body != null) {
RequestBodyEntity entity;
if (body instanceof JsonNode) {
entity = putReq.body((JsonNode) body);
} else {
ObjectMapper om = new ObjectMapper();
String bodyS = om.writeValueAsString(body);
entity = putReq.body(bodyS);
}
return entity;
}
return put(cmd, params, objs);
}
示例14: save
import com.mashape.unirest.request.HttpRequestWithBody; //导入依赖的package包/类
@Override public <T extends Event<?>> void save(T event) throws Exception {
Map<String, String> data = null;
if (event instanceof ChangeEvent) {
ChangeEvent e = (ChangeEvent) event;
data = handle(e);
if (data != null) {
String uri = this.props.get("uri") + "/metrics";
HttpRequestWithBody request = Unirest.post(uri);
request.queryString("metric", e.variable());
request.queryString("value", data.get("value"));
data.remove("value");
for (String tag : data.keySet()) {
request.queryString("label[" + tag + "]", data.get(tag));
}
HttpResponse<String> response = request.asString();
if (Boolean.valueOf(this.props.get("output_responses"))) {
System.out.println(response.getStatusText() + "\t"
+ response.getBody());
}
}
}
}
示例15: login
import com.mashape.unirest.request.HttpRequestWithBody; //导入依赖的package包/类
/**
* 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;
}