All Projects → hsiafan → Requests

hsiafan / Requests

Licence: bsd-2-clause
Convenient http client for java, inspired by python request module

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Requests

1c http
Подсистема 1С для работы с HTTP
Stars: ✭ 48 (-89.54%)
Mutual labels:  http-client, http-requests
centra
Core Node.js HTTP client
Stars: ✭ 52 (-88.67%)
Mutual labels:  http-client, http-requests
relay
Relay lets you write HTTP requests as easy to read, structured YAML and dispatch them easily using a CLI. Similar to tools like Postman
Stars: ✭ 22 (-95.21%)
Mutual labels:  http-client, http-requests
EthernetWebServer SSL
Simple TLS/SSL Ethernet WebServer, HTTP Client and WebSocket Client library for for AVR, Portenta_H7, Teensy, SAM DUE, SAMD21, SAMD51, STM32F/L/H/G/WB/MP1, nRF52 and RASPBERRY_PI_PICO boards using Ethernet shields W5100, W5200, W5500, ENC28J60 or Teensy 4.1 NativeEthernet/QNEthernet. It now supports Ethernet TLS/SSL Client. The library supports …
Stars: ✭ 40 (-91.29%)
Mutual labels:  http-client, http-requests
robotframework-httprequestlibrary
Robot Framework's library to test REST interfaces utilizing Apache HttpClient
Stars: ✭ 20 (-95.64%)
Mutual labels:  http-client, http-requests
pawn-requests
pawn-requests provides an API for interacting with HTTP(S) JSON APIs.
Stars: ✭ 56 (-87.8%)
Mutual labels:  http-client, http-requests
requester
The package provides a very thin wrapper (no external dependencies) for http.Client allowing the use of layers (middleware).
Stars: ✭ 14 (-96.95%)
Mutual labels:  http-client, http-requests
Restclient
🦄 Simple HTTP and REST client for Unity based on Promises, also supports Callbacks! 🎮
Stars: ✭ 675 (+47.06%)
Mutual labels:  http-client, http-requests
EthernetWebServer
This is simple yet complete WebServer library for AVR, Portenta_H7, Teensy, SAM DUE, SAMD21/SAMD51, nRF52, STM32, RP2040-based, etc. boards running Ethernet shields. The functions are similar and compatible to ESP8266/ESP32 WebServer libraries to make life much easier to port sketches from ESP8266/ESP32. Coexisting now with `ESP32 WebServer` and…
Stars: ✭ 118 (-74.29%)
Mutual labels:  http-client, http-requests
Redes
High-level network layer abstraction library written in Swift.
Stars: ✭ 16 (-96.51%)
Mutual labels:  http-client, http-requests
Frequest
FRequest - A fast, lightweight and opensource desktop application to make HTTP(s) requests
Stars: ✭ 130 (-71.68%)
Mutual labels:  http-client, http-requests
Http Shortcuts
Android app to create home screen shortcuts that trigger arbitrary HTTP requests
Stars: ✭ 329 (-28.32%)
Mutual labels:  http-client, http-requests
Easygo
基于Kotlin、OkHttp的声明式网络框架,像写HTML界面一样写网络调用代码
Stars: ✭ 40 (-91.29%)
Mutual labels:  http-client, http-requests
direwolf
Package direwolf is a convenient and easy to use http client written in Golang.
Stars: ✭ 44 (-90.41%)
Mutual labels:  http-client, http-requests
Gout
gout to become the Swiss Army Knife of the http client @^^@---> gout 是http client领域的瑞士军刀,小巧,强大,犀利。具体用法可看文档,如使用迷惑或者API用得不爽都可提issues
Stars: ✭ 749 (+63.18%)
Mutual labels:  http-client, http-requests
swish
C++ HTTP requests for humans
Stars: ✭ 52 (-88.67%)
Mutual labels:  http-client, http-requests
Httpu
The terminal-first http client
Stars: ✭ 619 (+34.86%)
Mutual labels:  http-client, http-requests
nativescript-http
The best way to do HTTP requests in NativeScript, a drop-in replacement for the core HTTP with important improvements and additions like proper connection pooling, form data support and certificate pinning
Stars: ✭ 32 (-93.03%)
Mutual labels:  http-client, http-requests
Node Request Retry
💂 Wrap NodeJS request module to retry http requests in case of errors
Stars: ✭ 330 (-28.1%)
Mutual labels:  http-client, http-requests
Karin
An elegant promise based HTTP client for the browser and node.js [WIP]
Stars: ✭ 393 (-14.38%)
Mutual labels:  http-client, http-requests

Requests is a http request lib with fluent api for java, inspired by the python request module. Requests requires JDK 1.8+, the last version support Java7 is 4.18.* .

Table of Contents

Maven Setting

Requests is now in maven central repo.

<dependency>
    <groupId>net.dongliu</groupId>
    <artifactId>requests</artifactId>
    <version>5.0.8</version>
</dependency>

Usage

Simple Case

One simple http request example that do http get request and read response as string:

String url = ...;
String resp = Requests.get(url).send().readToText();
// or
Response<String> resp = Requests.get(url).send().toTextResponse();

Post and other method:

resp = Requests.post(url).send().readToText();
resp = Requests.head(url).send().readToText();
...

The response object have several common http response fields can be used:

RawResponse resp = Requests.get(url).send();
int statusCode = resp.statusCode();
String contentLen = resp.getHeader("Content-Length");
Cookie cookie = resp.getCookie("_bd_name");
String body = resp.readToText();

Make sure call readToText or other methods to consume resp, or call close method to close resp.

The readToText() method here trans http response body as String, more other methods provided:

// get response as string, use encoding get from response header
String resp = Requests.get(url).send().readToText();
// get response as bytes
byte[] resp1 = Requests.get(url).send().readToBytes();
// save response as file
boolean result = Requests.get(url).send().writeToFile("/path/to/save/file");

Charset

Requests default use UTF-8 to encode parameters, post forms or request string body, you can set other charset by:

String resp = Requests.get(url).charset(StandardCharsets.ISO_8859_1).send().readToText();

When read response to text-based result, use charset get from http response header, or UTF-8 if not found. You can force use specified charset by:

String resp = Requests.get(url).send().charset(StandardCharsets.ISO_8859_1).readToText();

Passing Parameters

Pass parameters in urls using params method:

// set params by map
Map<String, Object> params = new HashMap<>();
params.put("k1", "v1");
params.put("k2", "v2");
String resp = Requests.get(url).params(params).send().readToText();
// set multi params
String resp = Requests.get(url)
        .params(Parameter.of("k1", "v1"), Parameter.of("k2", "v2"))
        .send().readToText();

If you want to send post www-form-encoded parameters, use body() methods:

// set params by map
Map<String, Object> params = new HashMap<>();
params.put("k1", "v1");
params.put("k2", "v2");
String resp = Requests.post(url).body(params).send().readToText();
// set multi params
String resp = Requests.post(url)
        .body(Parameter.of("k1", "v1"), Parameter.of("k2", "v2"))
        .send().readToText();

The forms parameter should only works with post method.

Set Headers

Http request headers can be set by headers method:

// set headers by map
Map<String, Object> headers = new HashMap<>();
headers.put("k1", "v1");
headers.put("k2", "v2");
String resp = Requests.get(url).headers(headers).send().readToText();
// set multi headers
String resp = Requests.get(url)
        .headers(new Header("k1", "v1"), new Header("k2", "v2"))
        .send().readToText();

Cookies

Cookies can be add by:

Map<String, Object> cookies = new HashMap<>();
cookies.put("k1", "v1");
cookies.put("k2", "v2");
// set cookies by map
String resp = Requests.get(url).cookies(cookies).send().readToText();
// set cookies
String resp = Requests.get(url)
        .cookies(Parameter.of("k1", "v1"), Parameter.of("k2", "v2"))
        .send().readToText();

Request with data

Http Post, Put, Patch method can send request body. Take Post for example:

// set post form data
String resp = Requests.post(url).body(Parameter.of("k1", "v1"), Parameter.of("k2", "v2"))
        .send().readToText();
// set post form data by map
Map<String, Object> formData = new HashMap<>();
formData.put("k1", "v1");
formData.put("k2", "v2");
String resp = Requests.post(url).body(formData).send().readToText();
// send byte array data as body
byte[] data = ...;
resp = Requests.post(url).body(data).send().readToText();
// send string data as body
String str = ...;
resp = Requests.post(url).body(str).send().readToText();
// send data from inputStream
InputStreamSupplier supplier = ...;
resp = Requests.post(url).body(supplier).send().readToText();

One more complicate situation is multiPart post request, this can be done via multiPart method, one simplified multi part request example which send files and param data:

// send form-encoded data
InputStreamSupplier supplier = ...;
byte[] bytes = ...;
String resp = Requests.post(url)
        .multiPartBody(
            Part.file("file1", new File(...)),
            Part.file("file2", "second_file.dat", supplier),
            Part.text("input", "on")
        ).send().readToText();

Json support

Requests can handle json encoder(for request body)/decoder(for response body), if having Json Binding, Jackson, Gson, or Fastjson lib in classpath.

// send json body, content-type is set to application/json
RawResponse response = Requests.post("http://.../update_person")
                .jsonBody(value)
                .send();
// response body as json, to value
Person person = Requests.post("http://.../get_person")
                .params(Parameter.of("id", 101))
                .send().readToJson(Person.class);
// json body decoder to generic type
List<Person> persons = Requests.post("http://.../get_person_list")
                .send().readToJson(new TypeInfer<List<Person>>() {});

You may set your own json processor by:

JsonProcessor jsonProcessor = ...;
JsonLookup.getInstance().register(jsonProcessor);

Basic Auth

Set http basic auth param by auth method:

String resp = Requests.get(url).basicAuth("user", "passwd").send().readToText();

Redirection

Requests will handle 30x http redirect automatically, you can disable it by:

Requests.get(url).followRedirect(false).send();

Timeout

You can set connection connect timeout, and socket read/write timeout value, as blow:

// set connect timeout and socket timeout
Requests.get(url).socksTimeout(20_000).connectTimeout(30_000).send();

Response compress encoding

Requests send Accept-Encoding: gzip, deflate, and auto handle response decompress in default. You can disable this by:

// do not send Accept-Encoding: gzip, deflate header
String resp = Requests.get(url).acceptCompress(false).send().readToText();
// do not decompress response body
String resp2 = Requests.get(url).send().decompress(false).readToText();

Https Verification

Some https sites do not have trusted http certificate, Exception will be thrown when request. You can disable https certificate verify by:

Requests.get(url).verify(false).send();

Proxy

Set proxy by proxy method:

Requests.get(url).proxy(Proxies.httpProxy("127.0.0.1", 8081)).send(); // http proxy
Requests.get(url).proxy(Proxies.socksProxy("127.0.0.1", 1080)).send(); // socks proxy proxy

Session

Session maintains cookies, basic auth and maybe other http context for you, useful when need login or other situations. Session have the same usage as Requests.

Session session = Requests.session();
String resp1 = session.get(url1).send().readToText();
String resp2 = session.get(url2).send().readToText();
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].