use of okhttp3.FormBody in project Douya by DreaminginCodeZH.
the class InterceptorUtils method addParameters.
public static void addParameters(Request.Builder builder, Request request, Map<String, String> parameters) throws IOException {
String method = request.method();
if (!HttpMethod.requiresRequestBody(method)) {
HttpUrl.Builder urlBuilder = request.url().newBuilder();
for (Map.Entry<String, String> parameter : parameters.entrySet()) {
String parameterName = parameter.getKey();
urlBuilder.removeAllQueryParameters(parameterName);
urlBuilder.addQueryParameter(parameterName, parameter.getValue());
}
builder.url(urlBuilder.build());
} else {
RequestBody body = request.body();
if (body == null || body.contentLength() == 0) {
FormBody.Builder bodyBuilder = new FormBody.Builder();
for (Map.Entry<String, String> parameter : parameters.entrySet()) {
bodyBuilder.add(parameter.getKey(), parameter.getValue());
}
body = bodyBuilder.build();
} else if (body instanceof FormBody) {
FormBody formBody = (FormBody) body;
FormBody.Builder bodyBuilder = new FormBody.Builder();
for (int i = 0, size = formBody.size(); i < size; ++i) {
String name = formBody.name(i);
if (parameters.containsKey(name)) {
continue;
}
bodyBuilder.add(name, formBody.value(i));
}
for (Map.Entry<String, String> parameter : parameters.entrySet()) {
bodyBuilder.add(parameter.getKey(), parameter.getValue());
}
body = bodyBuilder.build();
} else if (body instanceof MultipartBody) {
MultipartBody multipartBody = (MultipartBody) body;
MultipartBody.Builder bodyBuilder = new MultipartBody.Builder(multipartBody.boundary()).setType(multipartBody.type());
List<MultipartBody.Part> parameterParts = new ArrayList<>();
for (Map.Entry<String, String> parameter : parameters.entrySet()) {
MultipartBody.Part parameterPart = MultipartBody.Part.createFormData(parameter.getKey(), parameter.getValue());
parameterParts.add(parameterPart);
}
List<String> parameterContentDispositions = new ArrayList<>();
for (MultipartBody.Part parameterPart : parameterParts) {
String parameterContentDisposition = parameterPart.headers().get(Http.Headers.CONTENT_DISPOSITION);
parameterContentDispositions.add(parameterContentDisposition);
}
for (int i = 0, size = multipartBody.size(); i < size; ++i) {
MultipartBody.Part part = multipartBody.part(i);
Headers headers = part.headers();
if (headers != null) {
String contentDisposition = headers.get(Http.Headers.CONTENT_DISPOSITION);
if (parameterContentDispositions.contains(contentDisposition)) {
continue;
}
}
bodyBuilder.addPart(part);
}
for (MultipartBody.Part parameterPart : parameterParts) {
bodyBuilder.addPart(parameterPart);
}
body = bodyBuilder.build();
} else {
LogUtils.wtf("Unknown request body " + body.getClass().getName() + ": " + body);
}
builder.method(method, body).removeHeader(Http.Headers.CONTENT_LENGTH).header(Http.Headers.CONTENT_LENGTH, String.valueOf(body.contentLength()));
}
}
use of okhttp3.FormBody in project sonarqube by SonarSource.
the class HttpConnector method post.
private WsResponse post(PostRequest postRequest) {
HttpUrl.Builder urlBuilder = prepareUrlBuilder(postRequest);
RequestBody body;
Map<String, PostRequest.Part> parts = postRequest.getParts();
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(prepareOkHttpClient(noRedirectOkHttpClient, postRequest), okRequestBuilder.build());
response = checkRedirect(response, postRequest);
return new OkHttpResponse(response);
}
use of okhttp3.FormBody in project instagram-java-scraper by postaddictme.
the class Instagram method addMediaComment.
public ActionResponse<Comment> addMediaComment(String code, String commentText) throws IOException {
String url = Endpoint.addMediaCommentLink(MediaUtil.getIdFromCode(code));
FormBody formBody = new FormBody.Builder().add("comment_text", commentText).build();
Request request = new Request.Builder().url(url).header(Endpoint.REFERER, Endpoint.getMediaPageLinkByCode(code) + "/").post(formBody).build();
Response response = executeHttpRequest(withCsrfToken(request));
try (InputStream jsonStream = response.body().byteStream()) {
return mapper.mapMediaCommentResponse(jsonStream);
}
}
use of okhttp3.FormBody in project Auto.js by hyb1996.
the class ExampleUnitTest method test.
@Test
public void test() throws IOException {
File file = new File("C:\\Users\\Stardust\\Desktop\\1.txt");
System.out.println(PFiles.read(file));
String url = "http://posttestserver.com/post.php?dir=example";
OkHttpClient client = new OkHttpClient();
RequestBody formBody = new MultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart("file", file.getName(), RequestBody.create(MediaType.parse("text/plain"), file)).addFormDataPart("other_field", "other_field_value").build();
Request request = new Request.Builder().url(url).post(formBody).build();
Response response = client.newCall(request).execute();
System.out.println(response.body().string());
}
use of okhttp3.FormBody in project okhttp by square.
the class PostForm method run.
public void run() throws Exception {
RequestBody formBody = new FormBody.Builder().add("search", "Jurassic Park").build();
Request request = new Request.Builder().url("https://en.wikipedia.org/w/index.php").post(formBody).build();
try (Response response = client.newCall(request).execute()) {
if (!response.isSuccessful())
throw new IOException("Unexpected code " + response);
System.out.println(response.body().string());
}
}
Aggregations