add GsonRequest.body

This commit is contained in:
Timi
2025-10-29 15:23:55 +08:00
parent f3623d03a2
commit fbcf21e990
2 changed files with 12 additions and 0 deletions

View File

@ -7,6 +7,7 @@ import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.imyeyu.java.TimiJava;
import org.apache.hc.client5.http.fluent.Request;
import org.apache.hc.core5.http.ContentType;
import java.io.IOException;
@ -45,6 +46,11 @@ public class GsonRequest extends CommonRequest {
return this;
}
public GsonRequest body(Object object) {
request.bodyString(getGson().toJson(object), ContentType.APPLICATION_JSON);
return this;
}
public <T> T resultAs(Class<T> clazz) throws IOException {
return getGson().fromJson(super.asString(), clazz);
}

View File

@ -28,6 +28,12 @@ public class TimiRequest extends GsonRequest {
return new TimiRequest(Request.post(url));
}
@Override
public TimiRequest body(Object object) {
super.body(object);
return this;
}
@Override
public String asString() throws IOException {
return resultAs(String.class);