Skip to content
This repository was archived by the owner on Mar 7, 2019. It is now read-only.

Latest commit

 

History

History
61 lines (45 loc) · 2.55 KB

File metadata and controls

61 lines (45 loc) · 2.55 KB

Upgrading to Open Unirest 3.0 from previous versions

Package

Inspired by the "Java Spark" project, all classes are now in the unirest package. This project only has 45 files, it really doesn't need anything more complicated than that.

Configuration

Previous versions of unirest had configuration split across several different places. Sometimes it was done on Unirest, sometimes it was done on Option, sometimes it was somewhere else. All configuration is now done through Unirest.config()

Unirest.config()

Unirest config allows easy access to build a configuration just like you would build a request:

    Unirest.config()
           .socketTimeout(500)
           .connectTimeout(1000)
           .concurrency(10, 5)
           .proxy(new HttpHost("https://proxy"))
           .setDefaultHeader("Accept", "application/json")
           .followRedirects(false)
           .enableCookieManagement(false)
           .addInterceptor(new MyCustomInterceptor());
Changing the config

Changing Unirest's config should ideally be done once, or rarely. There are several background threads spawned by both Unirest itself and Apache HttpAsyncClient. Once Unirest has been activated configuration options that are involved in creating the client cannot be changed without an explicit shutdown or reset.

     Unirest.config()
            .reset()
            .connectTimeout(5000)
Setting custom Apache Client

You can set your own custom Apache HttpClient and HttpAsyncClient. Note that Unirest settings like timeouts or interceptors are not applied to custom clients.

     Unirest.config()
            .httpClient(myClient)
            .asyncClient(myAsyncClient)

Multiple Configuration Instances

As usual, Unirest maintains a primary single instance. Sometimes you might want different configurations for different systems. You might also want an instance rather than a static context for testing purposes.

    // this returns the same instance used by Unirest.get("http://somewhere/")
    UnirestInstance unirest = Unirest.primaryInstance(); 
    // It can be configured and used just like the static context
    unirest.config().connectTimeout(5000);
    String result = unirest.get("http://foo").asString().getBody();
    
    // You can also get a whole new instance
    UnirestInstance unirest = Unirest.spawnInstance();

WARNING! If you get a new instance of unirest YOU are responsible for shutting it down when the JVM shuts down. It is not tracked or shut down by Unirest.shutDown();