Search in sources :

Example 1 with FormBody

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);
}
Also used : Data2(com.shizhefei.mvc.data.Data2) MultipartBody(okhttp3.MultipartBody) RequestBody(okhttp3.RequestBody)

Example 2 with 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);
}
Also used : FormBody(okhttp3.FormBody) RequestBody(okhttp3.RequestBody)

Example 3 with 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);
}
Also used : FormBody(okhttp3.FormBody) RequestBody(okhttp3.RequestBody)

Example 4 with 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 + "]");
    }
}
Also used : Request(okhttp3.Request) FormBody(okhttp3.FormBody) JsonNode(com.fasterxml.jackson.databind.JsonNode) HttpUrl(okhttp3.HttpUrl) Response(okhttp3.Response) MediaType(okhttp3.MediaType) HashMap(java.util.HashMap) Map(java.util.Map) ConnectException(java.net.ConnectException)

Example 5 with FormBody

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);
}
Also used : Request(okhttp3.Request) Strings.nullToEmpty(com.google.common.base.Strings.nullToEmpty) Strings.isNullOrEmpty(com.google.common.base.Strings.isNullOrEmpty) IOException(java.io.IOException) Credentials(okhttp3.Credentials) HTTP_MOVED_PERM(java.net.HttpURLConnection.HTTP_MOVED_PERM) String.format(java.lang.String.format) HTTP_PERM_REDIRECT(okhttp3.internal.http.StatusLine.HTTP_PERM_REDIRECT) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) RequestBody(okhttp3.RequestBody) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) HTTP_MOVED_TEMP(java.net.HttpURLConnection.HTTP_MOVED_TEMP) FormBody(okhttp3.FormBody) OkHttpClient(okhttp3.OkHttpClient) HTTP_TEMP_REDIRECT(okhttp3.internal.http.StatusLine.HTTP_TEMP_REDIRECT) MultipartBody(okhttp3.MultipartBody) Proxy(java.net.Proxy) X509TrustManager(javax.net.ssl.X509TrustManager) Map(java.util.Map) Response(okhttp3.Response) Call(okhttp3.Call) HttpUrl(okhttp3.HttpUrl) Nullable(javax.annotation.Nullable) MediaType(okhttp3.MediaType) FormBody(okhttp3.FormBody) Request(okhttp3.Request) HttpUrl(okhttp3.HttpUrl) Response(okhttp3.Response) RequestBody(okhttp3.RequestBody)

Aggregations

Request (okhttp3.Request)59 Response (okhttp3.Response)56 FormBody (okhttp3.FormBody)53 RequestBody (okhttp3.RequestBody)41 IOException (java.io.IOException)37 Call (okhttp3.Call)32 Callback (okhttp3.Callback)29 JSONObject (org.json.JSONObject)18 Map (java.util.Map)15 HttpUrl (okhttp3.HttpUrl)10 HashMap (java.util.HashMap)8 OkHttpClient (okhttp3.OkHttpClient)8 MultipartBody (okhttp3.MultipartBody)7 ArrayList (java.util.ArrayList)6 TypeToken (com.google.gson.reflect.TypeToken)4 MediaType (okhttp3.MediaType)4 DataInputStream (java.io.DataInputStream)2 InputStream (java.io.InputStream)2 String.format (java.lang.String.format)2 ConnectException (java.net.ConnectException)2