Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/2.7.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
li-xunhuan committed Oct 9, 2023
2 parents 3b1e7fc + 35e6db6 commit 8d58c2a
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions mica-http/src/main/java/net/dreamlu/mica/http/HttpRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.X509TrustManager;
import java.io.File;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.ProxySelector;
Expand All @@ -57,7 +58,7 @@
*/
public class HttpRequest {
private static final String DEFAULT_USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36";
private static final MediaType APPLICATION_JSON = MediaType.get("application/json;charset=UTF-8");
private static final MediaType APPLICATION_JSON = MediaType.get("application/json");
private static volatile OkHttpClient httpClient = new OkHttpClient();
@Nullable
private static HttpLoggingInterceptor globalLoggingInterceptor = null;
Expand Down Expand Up @@ -211,20 +212,38 @@ public HttpRequest body(RequestBody requestBody) {
return this;
}

public HttpRequest body(byte[] body) {
return body(RequestBody.create(body));
}

public HttpRequest body(byte[] body, MediaType contentType) {
return body(RequestBody.create(body, contentType));
}

public HttpRequest body(File body, MediaType contentType) {
return body(RequestBody.create(body, contentType));
}

public HttpRequest body(String body, MediaType contentType) {
return body(RequestBody.create(body, contentType));
}

public HttpRequest bodyString(String body) {
this.requestBody = RequestBody.create (body, APPLICATION_JSON);
return this;
return body(RequestBody.create(body, APPLICATION_JSON));
}

public HttpRequest bodyString(MediaType contentType, String body) {
this.requestBody = RequestBody.create(body, contentType);
return this;
return body(RequestBody.create(body, contentType));
}

public HttpRequest bodyJson(Object body) {
return bodyString(JsonUtil.toJson(body));
}

public HttpRequest bodyJson(Object body, MediaType contentType) {
return body(JsonUtil.toJsonAsBytes(body), contentType);
}

private HttpRequest(final Request.Builder requestBuilder, String url, String httpMethod) {
HttpUrl httpUrl = HttpUrl.parse(url);
if (httpUrl == null) {
Expand Down

0 comments on commit 8d58c2a

Please sign in to comment.