User Tools
development:http_library
HTTP Library
— Avinash D'Silva 2019/04/12 09:25
The JavaHttpClient is a wrapper library developed as a part of R&D (in April 2019 IDMS-3748) so as to provide a HTTP client for our aging SnmpManager as it wasn't practical to upgrade the existing libraries.
The JavaHttpClient was created to deal with the following primary challenges:
- Allow connecting to web servers with bad SSL
- Allow to connect(using curl fallback) to newer SSL ciphers/protocols (currently not supported in java 1.7)
- Allow to post/patch/put raw JSON/binary (This was till now done through hacky workarounds)
- Support reading and writing raw header information.
Example usage:
import com.errigal.snmpmanager.autodiscovery.http.* String url = "" String body ="""{"hi":"bye"}""" CustomHttpResponse chr = null CustomHttpClient client = new JavaHttpClient() url = 'https://demo7260167.mockable.io/getTest' log.info "url: "+url chr = client.get(url,null) log.info chr.dump() assert chr.success == true url = 'https://demo7260167.mockable.io/optionsTest' log.info "url: "+url chr = client.options(url,null) log.info chr.dump() assert chr.success == true //head not testable url = 'https://demo7260167.mockable.io/deleteTest' log.info "url: "+url chr = client.delete(url,null) log.info chr.dump() assert chr.success == true url = 'https://demo7260167.mockable.io/postTest' log.info "url: "+url chr = client.post(url,body.getBytes(),'text/html',null) log.info chr.dump() assert chr.success == true url = 'https://demo7260167.mockable.io/putTest' log.info "url: "+url chr = client.put(url,body.getBytes(),'text/html',null) log.info chr.dump() assert chr.success == true url = 'https://demo7260167.mockable.io/patchTest' log.info "url: "+url chr = client.patch(url,body.getBytes(),'text/html',null) log.info chr.dump() assert chr.success == true
JavaHttpClient falls to CurlHttpClient if it is unable to process due to SSL specific exceptions.
CustomHttpResponse has two convenience methods :
There are some gotchas to remember due to the way RFC allows duplicate HTTP headers keys, The working is defined in the tests below
1. getCookie
void testResponse(){ CustomHttpResponse customHttpResponse = new CustomHttpResponse() customHttpResponse.getHeaders().put("k0","v0") customHttpResponse.getHeaders().put("sEt-CoOkIe","v1=x") customHttpResponse.getHeaders().put("SET-COOKIE","v2=y") String cookie = customHttpResponse.getCookie("v2") assert cookie == "v2=y" }
2. getHeader
void testGetHeader(){ CustomHttpResponse customHttpResponse = new CustomHttpResponse() customHttpResponse.getHeaders().put("k0","v0") customHttpResponse.getHeaders().put("k1","v1") customHttpResponse.getHeaders().put("k1","v2") assert customHttpResponse.getHeader("k0") == "v0" assert customHttpResponse.getHeader("k1") == "v1" }
development/http_library.txt · Last modified: 2021/06/25 10:09 by 127.0.0.1