GroundWork FoxHttp Examples - viascom/FoxHttp GitHub Wiki
FoxHttpRequest foxHttpRequest = new FoxHttpRequest();
foxHttpRequest.setUrl(new URL("http://httpbin.org/get"));
FoxHttpResponse response = foxHttpRequest.execute();
System.out.println(response.toString(true));
Query parameters are key-value pairs after the url.
Example:
http://httpbin.org/get?key=value&key2=value2
FoxHttpRequestQuery requestQuery = new FoxHttpRequestQuery();
requestQuery.addQueryEntry("project","groundwork");
requestQuery.addQueryEntry("id","123-456-789");
FoxHttpRequest foxHttpRequest = new FoxHttpRequest();
foxHttpRequest.setUrl(new URL("http://httpbin.org/get"));
foxHttpRequest.setRequestQuery(requestQuery);
FoxHttpResponse response = foxHttpRequest.execute();
System.out.println(response.getStringBody());
FoxHttpRequestHeader requestHeader = new FoxHttpRequestHeader();
requestHeader.addHeader("Client-Id","123-456-789");
FoxHttpRequest foxHttpRequest = new FoxHttpRequest();
foxHttpRequest.setUrl(new URL("http://httpbin.org/get"));
foxHttpRequest.setRequestHeader(requestHeader);
FoxHttpResponse response = foxHttpRequest.execute();
System.out.println(response.getStringBody());
The FoxHttpAuthorizationScope in this example contains wildcards (*), so it matches http and https as well as every addition after basic-auth/
FoxHttpClient foxHttpClient = new FoxHttpClient();
BasicAuthAuthorization basicAuthAuthorization = new BasicAuthAuthorization("user", "passwd");
FoxHttpAuthorizationScope foxHttpAuthorizationScope = FoxHttpAuthorizationScope.create("*://httpbin.org/basic-auth/*", RequestType.GET);
foxHttpClient.getFoxHttpAuthorizationStrategy().addAuthorization(foxHttpAuthorizationScope, basicAuthAuthorization);
FoxHttpRequest foxHttpRequest = new FoxHttpRequest(foxHttpClient);
foxHttpRequest.setUrl(new URL("http://httpbin.org/basic-auth/user/passwd"));
FoxHttpResponse response = foxHttpRequest.execute();
System.out.println(response.getStringBody());
FoxHttpRequestBody requestBody = new RequestStringBody("Request-Body", ContentType.DEFAULT_TEXT);
FoxHttpRequest foxHttpRequest = new FoxHttpRequest();
foxHttpRequest.setUrl(new URL("http://httpbin.org/post"));
foxHttpRequest.setRequestType(RequestType.POST);
foxHttpRequest.setRequestBody(requestBody);
FoxHttpResponse response = foxHttpRequest.execute();
System.out.println(response.getStringBody());
User-Object (uses Project Lombok)
@Data
public class User implements Serializable {
private String username = "[email protected]";
private String firstname = "Fox";
private String lastname = "Http";
}
Request
User user = new User();
FoxHttpClient foxHttpClient = new FoxHttpClient();
//Use built-in Gson-Parser to serialize the request object as json
foxHttpClient.setFoxHttpRequestParser(new GsonParser());
FoxHttpRequestBody requestBody = new RequestObjectBody(user);
FoxHttpRequest foxHttpRequest = new FoxHttpRequest(foxHttpClient);
foxHttpRequest.setUrl(new URL("http://httpbin.org/post"));
foxHttpRequest.setRequestType(RequestType.POST);
foxHttpRequest.setRequestBody(requestBody);
FoxHttpResponse response = foxHttpRequest.execute();
System.out.println(response.getStringBody());
User-Object (uses Project Lombok)
@Data
public class User implements Serializable {
private String username = "[email protected]";
private String firstname = "Fox";
private String lastname = "Http";
}
Request
User user = new User();
FoxHttpClient foxHttpClient = new FoxHttpClient();
//Use built-in Gson-Parser to serialize the request object as json
foxHttpClient.setFoxHttpRequestParser(new GsonParser());
//Use built-in Gson-Parser to deserialize the response object from json
foxHttpClient.setFoxHttpResponseParser(new GsonParser());
FoxHttpRequestBody requestBody = new RequestObjectBody(user);
FoxHttpRequest<User> foxHttpRequest = new FoxHttpRequest<>(foxHttpClient);
foxHttpRequest.setUrl(new URL("http://httpbin.org/post"));
foxHttpRequest.setRequestType(RequestType.POST);
foxHttpRequest.setRequestBody(requestBody);
FoxHttpResponse<User> response = foxHttpRequest.execute();
//Use the function getParsedBody() to the response as object
User responseUser = response.getParsedBody(User.class);
System.out.println(responseUser.getUsername());
FoxHttpRequest foxHttpRequest = new FoxHttpRequest();
foxHttpRequest.setRequestType(RequestType.GET);
foxHttpRequest.setUrl(new URL("http://httpbin.org/image/png"));
FoxHttpResponse foxHttpResponse = foxHttpRequest.execute();
final Path destination = Paths.get("/path/to/the/image/location/FoxHttp.png");
try (final InputStream in = foxHttpResponse.getInputStreamBody()) {
Files.copy(in, destination);
}
FoxHttpResponse foxHttpResponse = new FoxHttpRequestBuilder("http://httpbin.org/get").build().execute();
System.out.println(foxHttpResponse.getStringBody());
FoxHttpResponse foxHttpResponse = new FoxHttpRequestBuilder("http://httpbin.org/post", RequestType.POST)
.setRequestBody(new RequestStringBody("Request-Body", ContentType.DEFAULT_TEXT))
.build()
.execute();
System.out.println(foxHttpResponse.getStringBody());
Interceptor
public class XORInterceptor implements FoxHttpRequestBodyInterceptor {
private String key;
public XORInterceptor(String key) {
super();
this.key = key;
}
public byte[] xor(final byte[] input, final byte[] secret) {
final byte[] output = new byte[input.length];
if (secret.length == 0) {
throw new IllegalArgumentException("empty security key");
}
int spos = 0;
for (int pos = 0; pos < input.length; ++pos) {
output[pos] = (byte) (input[pos] ^ secret[spos]);
++spos;
if (spos >= secret.length) {
spos = 0;
}
}
return output;
}
@Override
public int getWeight() {
return 0;
}
@Override
public void onIntercept(FoxHttpRequestBodyInterceptorContext context) throws FoxHttpException {
//Get current body and save it as byte[]
byte[] body = xor(context.getRequestBody().getOutputStream().toByteArray(), key.getBytes());
//Reset body
context.getRequestBody().getOutputStream().reset();
try {
//Write ne encrypted body
context.getRequestBody().getOutputStream().write(body);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Request
FoxHttpResponse foxHttpResponse = new FoxHttpRequestBuilder("http://httpbin.org/post", RequestType.POST)
.setRequestBody(new RequestStringBody("Request-Body", ContentType.DEFAULT_TEXT))
.registerInterceptor(FoxHttpInterceptorType.REQUEST_BODY, new XORInterceptor("Secret-Key"))
.build()
.execute();
System.out.println(foxHttpResponse.toString(true));