本文整理汇总了Java中com.mashape.unirest.request.HttpRequestWithBody.body方法的典型用法代码示例。如果您正苦于以下问题:Java HttpRequestWithBody.body方法的具体用法?Java HttpRequestWithBody.body怎么用?Java HttpRequestWithBody.body使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.mashape.unirest.request.HttpRequestWithBody
的用法示例。
在下文中一共展示了HttpRequestWithBody.body方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: 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);
}
}
示例3: 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;
}
}
示例4: 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);
}
示例5: 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;
}
}
示例6: 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);
}
示例7: main
import com.mashape.unirest.request.HttpRequestWithBody; //导入方法依赖的package包/类
public static void main(String[] args) throws UnirestException {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
HttpRequestWithBody requestWithBody = Unirest.put("http://192.168.1.103:9200/get-together/new-events/4");
HttpRequestWithBody requestBody = requestWithBody.header("content-type", MediaType.APPLICATION_JSON_VALUE);
Map<String, Object> body = new HashMap<>();
body.put("name", "Late Night with Elasticsearch");
body.put("date", DateFormatUtils.ISO_8601_EXTENDED_DATETIME_TIME_ZONE_FORMAT.format(new Date()));
RequestBodyEntity requestBodyEntity = requestBody.body(gson.toJson(body));
System.out.println(gson.toJson(gson.fromJson(requestBodyEntity.asString().getBody(), Object.class)));
}
示例8: executeDelete
import com.mashape.unirest.request.HttpRequestWithBody; //导入方法依赖的package包/类
private BaseRequest executeDelete(String baseUrl, Object urlMap, Object bodyMap) {
HttpRequestWithBody delete = Unirest.delete(baseUrl + urlTemplate.execute(urlMap));
if (bodyTemplate != null)
return delete.body(bodyTemplate.execute(bodyMap));
return delete;
}
示例9: execute
import com.mashape.unirest.request.HttpRequestWithBody; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
public ResponseWrapper<RECEIVE> execute(LittlstarApiClient apiClient, SEND data, StringPair... parameters)
throws UnirestException {
HttpRequestWithBody request = new HttpRequestWithBody(httpMethod, apiClient.getTLD() + apiCall);
for(String key : routeParams) {
boolean found = false;
for(StringPair param : parameters) {
if(param.getKey().equals(key)) {
found = true;
request.routeParam(key, param.getValue());
break;
}
}
if(!found) {
throw new IllegalArgumentException("No value passed for required API call key "+key);
}
}
//always include the Application Token in the header if it's set
if(apiClient.getApplicationToken() != null) {
request.header("X-AppToken", apiClient.getApplicationToken());
}
//if required, include the authenticated user's Apikey in the header
if(requiresAuthentication) {
request.header("X-Apikey", apiClient.getUserApiKey());
}
SendWrapper<SEND> sendWrapper = new SendWrapper<SEND>(data);
//send the SendWrapper as JSON Payload, unless EmptyData is wrapped
if(sendWrapper.toJson() != null) {
request.header("content-type", "application/json")
.body(sendWrapper.toJson());
} else {
//we need to put an empty body to execute the call
request.body("");
}
String json = "";
if(request.getBody() instanceof RequestBodyEntity && ((RequestBodyEntity)request.getBody()).getBody() != null) {
json = request.asString().getBody();
}
return LittlstarApiClient.getGson(responseClass).fromJson(json, ResponseWrapper.class);
}
示例10: requestAsText
import com.mashape.unirest.request.HttpRequestWithBody; //导入方法依赖的package包/类
/**
* Perform a request to the API with a text/plain body
*
* @param httpMethod
* @param url path of request
* @param body
* @param opts
* @return the request response
*/
public JsonNode requestAsText(HttpMethod httpMethod, String url, String body, RequestOptions opts) {
logger.debug("{} text/plain {}", httpMethod, url);
HttpRequestWithBody req = createRequest(httpMethod, url, opts);
req.body(body);
return tryRequest(req, opts);
}
示例11: makePostRequest
import com.mashape.unirest.request.HttpRequestWithBody; //导入方法依赖的package包/类
/**
* Method which uses OKHTTP to send a POST request to the specified URL saved
* within the APIMethod class
* @throws UnirestException
*/
@SuppressWarnings("unchecked")
private <T> T makePostRequest(ApiMethod method) throws RobinhoodApiException {
HttpRequestWithBody request = Unirest.post(method.getBaseUrl());
//Append each of the headers for the method
Iterator<HttpHeaderParameter> headerIterator = method.getHttpHeaderParameters().iterator();
while(headerIterator.hasNext()) {
HttpHeaderParameter currentHeader = headerIterator.next();
request.header(currentHeader.getKey(), currentHeader.getValue());
}
try {
//Append the request body
request.body(method.getUrlParametersAsPostBody());
//Make the request
HttpResponse<JsonNode> jsonResponse = request.asJson();
//Parse the response with Gson
Gson gson = new Gson();
String responseJsonString = jsonResponse.getBody().toString();
//If the response type for this is VOID (Meaning we are not expecting a response) do not
//try to use Gson
if(method.getReturnType() == Void.TYPE)
return (T) Void.TYPE;
T data = gson.fromJson(responseJsonString, method.getReturnType());
return data;
} catch (UnirestException ex) {
System.err.println("[RobinhoodApi] Failed to communicate with Robinhood servers, request failed");
}
throw new RobinhoodApiException();
}