use of okhttp3.FormBody in project MVCHelper by LuckyJayce.
the class PostFileMethod method buildRequset.
@Override
protected Request.Builder buildRequset(String url, Map<String, Object> params) {
MultipartBody.Builder builder = new MultipartBody.Builder();
if (params != null) {
for (Entry<String, ?> entry : params.entrySet()) {
builder.addFormDataPart(entry.getKey(), String.valueOf(entry.getValue()));
}
}
if (httpbodys != null) {
for (Entry<String, Data2<String, RequestBody>> entry : httpbodys.entrySet()) {
String key = entry.getKey();
Data2<String, RequestBody> body = entry.getValue();
builder.addFormDataPart(key, body.getValue1(), body.getValue2());
}
}
RequestBody formBody = builder.build();
if (listener != null) {
formBody = new CountingRequestBody(formBody, listener);
}
return new Request.Builder().url(url).post(formBody);
}
use of okhttp3.FormBody in project MVCHelper by LuckyJayce.
the class PostMethod method buildRequset.
@Override
protected Request.Builder buildRequset(String url, Map<String, Object> params) {
FormBody.Builder builder = new FormBody.Builder();
if (params != null) {
for (Entry<String, ?> entry : params.entrySet()) {
builder.add(entry.getKey(), String.valueOf(entry.getValue()));
}
}
RequestBody formBody = builder.build();
return new Request.Builder().url(url).post(formBody);
}
use of okhttp3.FormBody in project MVCHelper by LuckyJayce.
the class PutMethod method buildRequset.
@Override
protected Request.Builder buildRequset(String url, Map<String, Object> params) {
FormBody.Builder builder = new FormBody.Builder();
if (params != null) {
for (Entry<String, ?> entry : params.entrySet()) {
builder.add(entry.getKey(), String.valueOf(entry.getValue()));
}
}
RequestBody formBody = builder.build();
return new Request.Builder().url(url).put(formBody);
}
use of okhttp3.FormBody in project ignite by apache.
the class RestExecutor method sendRequest.
/** */
private RestResult sendRequest(boolean demo, String path, Map<String, Object> params, String mtd, Map<String, Object> headers, String body) throws IOException {
if (demo && AgentClusterDemo.getDemoUrl() == null) {
try {
AgentClusterDemo.tryStart().await();
} catch (InterruptedException ignore) {
throw new IllegalStateException("Failed to execute request because of embedded node for demo mode is not started yet.");
}
}
String url = demo ? AgentClusterDemo.getDemoUrl() : nodeUrl;
HttpUrl.Builder urlBuilder = HttpUrl.parse(url).newBuilder();
if (path != null)
urlBuilder.addPathSegment(path);
final Request.Builder reqBuilder = new Request.Builder();
if (headers != null) {
for (Map.Entry<String, Object> entry : headers.entrySet()) if (entry.getValue() != null)
reqBuilder.addHeader(entry.getKey(), entry.getValue().toString());
}
if ("GET".equalsIgnoreCase(mtd)) {
if (params != null) {
for (Map.Entry<String, Object> entry : params.entrySet()) {
if (entry.getValue() != null)
urlBuilder.addQueryParameter(entry.getKey(), entry.getValue().toString());
}
}
} else if ("POST".equalsIgnoreCase(mtd)) {
if (body != null) {
MediaType contentType = MediaType.parse("text/plain");
reqBuilder.post(RequestBody.create(contentType, body));
} else {
FormBody.Builder formBody = new FormBody.Builder();
if (params != null) {
for (Map.Entry<String, Object> entry : params.entrySet()) {
if (entry.getValue() != null)
formBody.add(entry.getKey(), entry.getValue().toString());
}
}
reqBuilder.post(formBody.build());
}
} else
throw new IllegalArgumentException("Unknown HTTP-method: " + mtd);
reqBuilder.url(urlBuilder.build());
try (Response resp = httpClient.newCall(reqBuilder.build()).execute()) {
String content = resp.body().string();
if (resp.isSuccessful()) {
JsonNode node = mapper.readTree(content);
int status = node.get("successStatus").asInt();
switch(status) {
case STATUS_SUCCESS:
return RestResult.success(node.get("response").toString());
default:
return RestResult.fail(status, node.get("error").asText());
}
}
if (resp.code() == 401)
return RestResult.fail(STATUS_AUTH_FAILED, "Failed to authenticate in grid. Please check agent\'s login and password or node port.");
return RestResult.fail(STATUS_FAILED, "Failed connect to node and execute REST command.");
} catch (ConnectException ignore) {
throw new ConnectException("Failed connect to node and execute REST command [url=" + urlBuilder + "]");
}
}
use of okhttp3.FormBody in project sonarlint-core by SonarSource.
the class HttpConnector method postInternal.
public WsResponse postInternal(PostRequest postRequest, @Nullable String bodyStr) {
HttpUrl.Builder urlBuilder = prepareUrlBuilder(postRequest);
RequestBody body;
Map<String, PostRequest.Part> parts = postRequest.getParts();
if (bodyStr != null) {
body = RequestBody.create(MediaType.parse(postRequest.getMediaType()), bodyStr);
} else if (parts.isEmpty()) {
// parameters are defined in the body (application/x-www-form-urlencoded)
FormBody.Builder formBody = new FormBody.Builder();
postRequest.getParameters().getKeys().forEach(key -> postRequest.getParameters().getValues(key).forEach(value -> formBody.add(key, value)));
body = formBody.build();
} else {
// parameters are defined in the URL (as GET)
completeUrlQueryParameters(postRequest, urlBuilder);
MultipartBody.Builder bodyBuilder = new MultipartBody.Builder().setType(MultipartBody.FORM);
parts.entrySet().forEach(param -> {
PostRequest.Part part = param.getValue();
bodyBuilder.addFormDataPart(param.getKey(), part.getFile().getName(), RequestBody.create(MediaType.parse(part.getMediaType()), part.getFile()));
});
body = bodyBuilder.build();
}
Request.Builder okRequestBuilder = prepareOkRequestBuilder(postRequest, urlBuilder).post(body);
Response response = doCall(noRedirectOkHttpClient, okRequestBuilder.build());
response = checkRedirect(response);
return new OkHttpResponse(response);
}
Aggregations