本文整理汇总了Java中com.mashape.unirest.request.HttpRequestWithBody.fields方法的典型用法代码示例。如果您正苦于以下问题:Java HttpRequestWithBody.fields方法的具体用法?Java HttpRequestWithBody.fields怎么用?Java HttpRequestWithBody.fields使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.mashape.unirest.request.HttpRequestWithBody
的用法示例。
在下文中一共展示了HttpRequestWithBody.fields方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: download
import com.mashape.unirest.request.HttpRequestWithBody; //导入方法依赖的package包/类
@Override
public HttpResponse download(HttpRequest request) throws DownloaderException {
if(log.isDebugEnabled()) {
log.debug("downloading..." + request.getUrl());
}
try {
HttpHost proxy = Proxys.getProxy();
if(proxy != null) {
Unirest.setProxy(proxy);
} else {
Unirest.setProxy(null);
}
request.addHeader("User-Agent", UserAgent.getUserAgent());
com.mashape.unirest.http.HttpResponse<String> response = null;
if(request instanceof HttpPostRequest) {
HttpPostRequest post = (HttpPostRequest)request;
HttpRequestWithBody httpRequestWithBody = Unirest.post(post.getUrl());
httpRequestWithBody.headers(post.getHeaders());
httpRequestWithBody.fields(post.getFields());
response = httpRequestWithBody.asString();
} else {
response = Unirest.get(request.getUrl()).headers(request.getHeaders()).asString();
}
String contentType = response.getHeaders().getFirst("Content-Type");
HttpResponse resp = new HttpResponse();
resp.setStatus(response.getStatus());
resp.setRaw(response.getRawBody());
resp.setContent(response.getBody());
resp.setContentType(contentType);
resp.setCharset(getCharset(request, contentType));
return resp;
} catch (UnirestException e) {
throw new DownloaderException(e);
}
}
示例2: post
import com.mashape.unirest.request.HttpRequestWithBody; //导入方法依赖的package包/类
/**
* Makes a POST request to the API.
*
* @param path the path relative to the API
* @param fields the fields for the request
* @param includePrivateToken if the private token should be added to the fields
* @return an HTTP response containing a JSON body
* @throws GitLabApiException if the request failed
*/
protected HttpResponse<JsonNode> post(String path, Map<String, Object> fields, boolean includePrivateToken)
throws GitLabApiException {
HttpRequestWithBody request = Unirest.post(getApiUrl() + path);
final MultipartBody body = request.fields(fields);
if (includePrivateToken) {
body.field("private_token", privateToken);
}
try {
// make request
return processPostResponse(request.asJson());
} catch (UnirestException e) {
throw new ApiConnectionFailureException("Could not connect to API", e);
}
}
示例3: post
import com.mashape.unirest.request.HttpRequestWithBody; //导入方法依赖的package包/类
/**
* Make a [POST]
* @param url
* @param data
* @param authentication
* @return resposne Code
* @throws UnirestException
*/
public int post(String url, Map<String, Object> data, boolean authentication) throws UnirestException{
Map<String, String> header = new HashMap<>();
header.put("accept", "application/json");
if (authentication) {
header.put("Authorization", "Token " + token);
}
HttpRequestWithBody h = Unirest.post(getAPIURL(url));
h.headers(header);
h.fields(data);
if (url.equals("login/")) {
try {
JSONObject j = h.asJson().getBody().getObject();
this.token = j.get("token").toString();
} catch (JSONException e) {
e.printStackTrace();
}
}
return h.asString().getCode();
}
示例4: executePostQuery
import com.mashape.unirest.request.HttpRequestWithBody; //导入方法依赖的package包/类
/**
* Test the {@link FreesoundClient#executeQuery(Query)} method, to ensure it correctly constructs and submits an
* HTTP POST request, and processes the results.
*
* @param mockUnirest Mock version of the {@link Unirest} library
* @param mockPostRequest Mock {@link HttpRequestWithBody} representing a POST call
* @param mockHttpResponse Mock {@link HttpResponse}
* @param mockResultsMapper Mock {@link SoundMapper}
*
* @throws Exception Any exceptions thrown in test
*/
@SuppressWarnings("static-access")
@Test
public void executePostQuery(
@Mocked final Unirest mockUnirest,
@Mocked final HttpRequestWithBody mockPostRequest,
@Mocked final HttpResponse<JsonNode> mockHttpResponse,
@Mocked final SoundMapper mockResultsMapper) throws Exception {
final Sound sound = new Sound();
new Expectations() {
{
mockUnirest.post(FreesoundClient.API_ENDPOINT + TEST_PATH); result = mockPostRequest;
mockPostRequest.header("Authorization", "Token " + CLIENT_SECRET);
mockPostRequest.routeParam(ROUTE_ELEMENT, ROUTE_ELEMENT_VALUE);
mockPostRequest.fields(with(new Delegate<HashMap<String, Object>>() {
@SuppressWarnings("unused")
void checkRequestParameters(final Map<String, Object> queryParameters) {
assertNotNull(queryParameters);
assertTrue(queryParameters.size() == 1);
assertEquals(QUERY_PARAMETER_VALUE, queryParameters.get(QUERY_PARAMETER));
}
}));
mockPostRequest.asJson(); result = mockHttpResponse;
mockHttpResponse.getStatus(); result = 200;
mockResultsMapper.map(mockHttpResponse.getBody().getObject()); result = sound;
}
};
final JSONResponseQuery<Sound> query = new TestJSONResponseQuery(HTTPRequestMethod.POST, mockResultsMapper);
final Response<Sound> response = freesoundClient.executeQuery(query);
assertSame(sound, response.getResults());
}
示例5: refreshAccessToken
import com.mashape.unirest.request.HttpRequestWithBody; //导入方法依赖的package包/类
/**
* Test that requests to renew an OAuth2 bearer token are correctly constructed and passed to the appropriate
* endpoint.
*
* @param mockUnirest Mock version of the {@link Unirest} library
* @param mockTokenRequest Mock {@link HttpRequestWithBody} representing a POST call to token endpoint
* @param mockHttpResponse Mock {@link HttpResponse}
*
* @throws Exception Any exceptions thrown in test
*/
@SuppressWarnings("static-access")
@Test
public void refreshAccessToken(
@Mocked final Unirest mockUnirest,
@Mocked final HttpRequestWithBody mockTokenRequest,
@Mocked final HttpResponse<JsonNode> mockHttpResponse) throws Exception {
new Expectations() {
{
mockUnirest.post(FreesoundClient.API_ENDPOINT + AccessTokenQuery.OAUTH_TOKEN_ENDPOINT_PATH);
result = mockTokenRequest;
mockTokenRequest.fields(with(new Delegate<HashMap<String, Object>>() {
@SuppressWarnings("unused")
void checkRequestParameters(final Map<String, Object> queryParameters) {
assertEquals(CLIENT_ID, queryParameters.get(AccessTokenQuery.CLIENT_ID_PARAMETER_NAME));
assertEquals(CLIENT_SECRET, queryParameters.get(AccessTokenQuery.CLIENT_SECRET_PARAMETER_NAME));
assertEquals(
RefreshOAuth2AccessTokenRequest.GRANT_TYPE,
queryParameters.get(AccessTokenQuery.GRANT_TYPE_PARAMETER_NAME));
assertEquals(
OAUTH_REFRESH_TOKEN,
queryParameters.get(RefreshOAuth2AccessTokenRequest.CODE_PARAMETER_NAME));
}
}));
mockTokenRequest.asJson(); result = mockHttpResponse;
mockHttpResponse.getStatus(); result = 200;
mockHttpResponse.getBody().getObject(); result = OAUTH_TOKEN_DETAILS_JSON;
}
};
final Response<AccessTokenDetails> response = freesoundClient.refreshAccessToken(OAUTH_REFRESH_TOKEN);
final AccessTokenDetails accessTokenDetails = response.getResults();
assertEquals(OAUTH_ACCESS_TOKEN, accessTokenDetails.getAccessToken());
assertEquals(OAUTH_REFRESH_TOKEN, accessTokenDetails.getRefreshToken());
assertEquals(OAUTH_SCOPE, accessTokenDetails.getScope());
assertEquals(OAUTH_TOKEN_EXPIRES_IN, accessTokenDetails.getExpiresIn());
}
示例6: patch
import com.mashape.unirest.request.HttpRequestWithBody; //导入方法依赖的package包/类
/**
* Make a [PATCH]
* @param url
* @param data
* @return response Code
* @throws UnirestException
*/
public int patch(String url, Map<String, Object> data) throws UnirestException {
Map<String, String> header = new HashMap<>();
header.put("accept", "application/json");
header.put("Authorization", "Token " + token);
HttpRequestWithBody h = Unirest.patch(getAPIURL(url));
h.headers(header);
h.fields(data);
return h.asJson().getCode();
}
示例7: requestAccessToken
import com.mashape.unirest.request.HttpRequestWithBody; //导入方法依赖的package包/类
/**
* Test that requests to redeem an authorisation code for an OAuth2 bearer token are correctly constructed and
* passed to the appropriate endpoint.
*
* @param mockUnirest Mock version of the {@link Unirest} library
* @param mockTokenRequest Mock {@link HttpRequestWithBody} representing a POST call to token endpoint
* @param mockHttpResponse Mock {@link HttpResponse}
*
* @throws Exception Any exceptions thrown in test
*/
@SuppressWarnings("static-access")
@Test
public void requestAccessToken(
@Mocked final Unirest mockUnirest,
@Mocked final HttpRequestWithBody mockTokenRequest,
@Mocked final HttpResponse<JsonNode> mockHttpResponse) throws Exception {
new Expectations() {
{
mockUnirest.post(FreesoundClient.API_ENDPOINT + AccessTokenQuery.OAUTH_TOKEN_ENDPOINT_PATH);
result = mockTokenRequest;
mockTokenRequest.fields(with(new Delegate<HashMap<String, Object>>() {
@SuppressWarnings("unused")
void checkRequestParameters(final Map<String, Object> queryParameters) {
assertEquals(CLIENT_ID, queryParameters.get(AccessTokenQuery.CLIENT_ID_PARAMETER_NAME));
assertEquals(CLIENT_SECRET, queryParameters.get(AccessTokenQuery.CLIENT_SECRET_PARAMETER_NAME));
assertEquals(
OAuth2AccessTokenRequest.GRANT_TYPE,
queryParameters.get(AccessTokenQuery.GRANT_TYPE_PARAMETER_NAME));
assertEquals(
OAUTH_AUTHORISATION_CODE,
queryParameters.get(OAuth2AccessTokenRequest.CODE_PARAMETER_NAME));
}
}));
mockTokenRequest.asJson(); result = mockHttpResponse;
mockHttpResponse.getStatus(); result = 200;
mockHttpResponse.getBody().getObject(); result = OAUTH_TOKEN_DETAILS_JSON;
}
};
final Response<AccessTokenDetails> response =
freesoundClient.redeemAuthorisationCodeForAccessToken(OAUTH_AUTHORISATION_CODE);
final AccessTokenDetails accessTokenDetails = response.getResults();
assertEquals(OAUTH_ACCESS_TOKEN, accessTokenDetails.getAccessToken());
assertEquals(OAUTH_REFRESH_TOKEN, accessTokenDetails.getRefreshToken());
assertEquals(OAUTH_SCOPE, accessTokenDetails.getScope());
assertEquals(OAUTH_TOKEN_EXPIRES_IN, accessTokenDetails.getExpiresIn());
}
示例8: post
import com.mashape.unirest.request.HttpRequestWithBody; //导入方法依赖的package包/类
/**
* Makes POST RESful API method based on {@link HbPost}
*
* @param cmd
* API command of type {@link HbPost}
* @param params
* to fill request with
* @param objs
* objects to replace in url
* @return {@link HttpRequestWithBody}
* @throws Exception
*/
public BaseRequest post(HbPost cmd, Params params, String... objs)
throws Exception {
HttpRequestWithBody postReq = post(apiLink + cmd.getCmd(objs));
MultipartBody multipartBody = postReq.fields(params.getAll());
return multipartBody;
}
示例9: 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
* @return {@link HttpRequestWithBody}
* @throws Exception
*/
public BaseRequest put(HbPut cmd, Params params,
String... objs)
throws Exception {
HttpRequestWithBody putReq = put(apiLink + cmd.getCmd(objs));
MultipartBody multipartBody = putReq.fields(params.getAll());
return multipartBody;
}