An open API service indexing awesome lists of open source software.

https://github.com/josdejong/httputil

Easily perform HTTP requests in Java
https://github.com/josdejong/httputil

Last synced: about 1 year ago
JSON representation

Easily perform HTTP requests in Java

Awesome Lists containing this project

README

          

httputil
========

Easily perform HTTP requests in Java

HttpUtil is a single class containing methods to conveniently perform HTTP
requests. HttpUtil only uses regular java io and net functionality and does
not depend on external libraries.
The class contains methods to perform a get, post, put, and delete request,
and supports posting forms. Optionally, one can provide headers.

## Usage

Insert the class [HttpUtil.java](https://raw.github.com/josdejong/httputil/master/src/com/almende/util/HttpUtil.java) in your project.

Next, the HttpUtil methods can called statically. An GET request looks like:

String res = HttpUtil.get("http://www.google.com");

## Examples

// GET
String res = HttpUtil.get("http://www.google.com");

// POST
String res = HttpUtil.post("http://sendmedata.com", "This is the data");

// POST JSON
String json = "{\"firstname\":\"Joe\",\"lastname\":\"Smith\",\"age\":\"28\"}";
String res = HttpUtil.postJson("http://sendmedata.com", json);

// POST FORM
Map params = new HashMap();
params.put("firstname", "Joe");
params.put("lastname", "Smith");
params.put("age", "28");
String res = HttpUtil.postForm("http://site.com/newuser", params);

// append query parameters to url
String url = "http://mydatabase.com/users";
Map params = new HashMap();
params.put("orderby", "name");
params.put("limit", "10");
String fullUrl = HttpUtil.appendQueryParams(url, params);
// fullUrl = "http://mydatabase.com/user?orderby=name&limit=10"

## API

HttpUtil contains the following static methods:

String get(String url)
String get(String url, Map headers)

String post(String url, String body)
String post(String url, String body, Map headers)

String postJson(String url, String jsonStr)

String postForm(String url, Map params)
String postForm(String url, Map params, Map headers)

String put(String url, String body)
String put(String url, String body, Map headers)

String delete(String url)
String delete(String url, Map headers)

fetch(String method, String url, String body, Map headers)

String appendQueryParams(String url, Map params)
String removeQueryParams(String url)
Map getQueryParams(String url)