openunirest / unirest-java Goto Github PK
View Code? Open in Web Editor NEWUnirest in Java: Simplified, lightweight HTTP client library.
License: Other
Unirest in Java: Simplified, lightweight HTTP client library.
License: Other
Asked because https://github.com/Kong/unirest-java is outdated compared to this one.
Unirest can use the previously set options by modifying Options.setDefaults to only override the options/interceptors etc if they aren't set
I reported this originally here: Kong/unirest-java#214
This is a bug issue for me where i want to use the same UniRest client to connect to multiple sites with different auth.
https://stackoverflow.com/questions/45304424/unirest-is-caching-basic-authentication-information
Already done in this PR. Kong/unirest-java#139
Typically errors like 404 or 403 will not return a JSON body (even when asking for JSON)
Rather return the response object with a field for the exception.
This would be a breaking change for a major version bump
It doesn't now because it it makes external web service calls to things that don't work like that anymore
That's just rude
Hey,
I would recommend forking/pullling/looking into @josueeduardo's unirest fork https://github.com/josueeduardo/rest-client and continuing to work at this point because all the much needed improvements were made there.
Otherwise, I think it would be a setback to discard so much improvements.
Need example
https://github.com/OpenUnirest/unirest-java#configuration shows some code snippets to do config that are static (pervasive for subsquent Unirest.post(...)
invocations.
Any chance on an alternate way of working where that config is instance based - rather than static/pervasive?
unirest = Unirest.clientWithConfig().socketTimeout(500).build();
unirest.post(...)...
I appreciate there's reset()
but that doesn't sit right with me having spend a career fighting static state.
Hi,
I just found this library and it looks like it would fit my application's needs, but as I am poking around I was wondering how well Unirest can handle being called multiple time concurrently each time connecting to a different REST server?
A bit of background about the application I am working on ... its primary purpose to query X number of application servers to gather their configuration information so that a user has a 'single pane of glass' view of all their application configurations. From a numbers standpoint it needs to be able to reach out to 2 - 400 different servers concurrently.
Thanks in advance!
Sean
Hi,
I got a java.lang.OutOfMemoryError when downloading a 2GB zip file with io.github.openunirest.request.BaseRequest.asBinary
.
Here is the StackTrace:
java.lang.OutOfMemoryError: null at java.io.ByteArrayOutputStream.hugeCapacity(ByteArrayOutputStream.java:123) at java.io.ByteArrayOutputStream.grow(ByteArrayOutputStream.java:117) at java.io.ByteArrayOutputStream.ensureCapacity(ByteArrayOutputStream.java:93) at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:153) at io.github.openunirest.http.utils.ResponseUtils.getBytes(ResponseUtils.java:51) at io.github.openunirest.http.utils.ResponseUtils.getRawBody(ResponseUtils.java:85) at io.github.openunirest.http.BodyData.<init>(BodyData.java:38) at io.github.openunirest.http.BodyData.from(BodyData.java:21) at io.github.openunirest.request.ResponseBuilder.asBinary(ResponseBuilder.java:25) at io.github.openunirest.request.HttpClientHelper.request(HttpClientHelper.java:34) at io.github.openunirest.request.BaseRequest.asBinary(BaseRequest.java:95)
Problem is the ByteArrayOutputStream which holds the whole file in memory.
E.g. with Jersey its working. Jersey uses mimepull library.
org.jvnet.mimepull.DataHead$ReadMultiStream
Maybe it would be a nice improvement to use the same? Or any other equivalent?
copied from Kong/unirest-java#213
Not sure if this lib is built on JDK 8, but, if it is, it'd be nice to use CompletableFuture instead of the old Future class:
CompletableFuture<HttpResponse<String>> eventualResponse = Unirest.get("http://").asStringFuture();
eventualResponse.thenAccept(response -> {
//code over here
});
Since Unirest has already a Callback interface, it'd be easy to convert it into a CompletableFuture:
public class CallbackPromise<T> extends CompletableFuture<HttpResponse<T>> implements Callback<T> {
@Override
public void completed(HttpResponse<T> response) {
complete(response);
}
@Override
public void failed(Exception e) {
completeExceptionally(e);
}
@Override
public void cancelled() {
completeExceptionally(new UnirestException("cancelled"));
}
}
Should also support the ability to inject your own JSON mapper in a way similar to feign. In fact, look into just supporting feigns interface for this
Since this not required to use the library (if you dont make async request), this is better to make this dep optional.
HttpResponse.java
line 101, 103,
} else if (objectMapper != null) {
this.body = objectMapper.readValue(new String(rawBody, charset), responseClass);
} else {
throw new Exception("Only String, JsonNode and InputStream are supported, or an ObjectMapper implementation is required.");
}
if the objectmapper throw exception or new Exception is throw, the exception is catcher and a new RuntimeException will be throw out.
then the line 110, EntityUtils.consume(responseEntity); will never be executed.
i believe, it should be clean up, before throw the RuntimeException
Checked exceptions suck
affected version: 1.4.9
Problem: The default multipart charset from Apache Http Client is 'US-ASCII' which destroys UTF-8 chars.
Solution: Please add the possibility to set a charset in com.mashape.unirest.request.body.MultipartBody.
A builder-style set method like mode(String) for the HttpEntity charset would be great.
Changes in com.mashape.unirest.request.body.MultipartBody:
add a new method 'charset(Charset)'
public MultipartBody charset(Charset charset) {
this.charset = charset;
return this;
}
set the charset in the getEntity-Method
...
builder.setCharset(this.charset);
...
Hello.
I used to use unirest very well.
Anyway, I have a problem with using queryString but I think I have a problem with it.
my code....
Map<String, Object> queryParams = new HashMap<>();
queryParams.put("pattern", null);
HttpResponse<String> response = null;
try {
response = Unirest.post(host)
.header("Content-Type", "application/json; charset=UTF-8")
.queryString(queryParams)
.body(body)
.asString();
} catch (UnirestException e) {
log.error(e.toString(), e);
}
The above code produces NPE.
Because if you look at the code, you can see the following
public HttpRequest queryString(Map<String, Object> parameters) {
if (parameters != null) {
for (Entry<String, Object> param : parameters.entrySet()) {
if (param.getValue() instanceof String || param.getValue() instanceof Number || param.getValue() instanceof Boolean) {
queryString(param.getKey(), param.getValue());
} else {
throw new RuntimeException("Parameter \"" + param.getKey() + "\" can't be sent with a GET request because of type: " + param.getValue().getClass().getName());
}
}
}
return this;
}
NPE was catched throw new RuntimeException("Parameter "" + param.getKey() + "" can't be sent with a GET request because of type: " + param.getValue().getClass().getName());
So... I think you need to process when param.getValue() is null. What do you think ?..
Thank you.
cc @kang-junyoung
Sure Headers is better with a has-a of TreeMap<String, List<Headers.Entry>> than the old pre-fork version of UniRest that was an is-a. It could give you a list of all of the headers, and allowed you to iterate thru them. That's gone from the current version.
The method size() is present in the current implementation, but there's nothing you can do with that. not get(index) or anything.
Unirest.post("https://dweet.io/dweet").fields(map).asJson(); // map is a HashMap of course
I am making a standard POST request to a server and I get the following while receiving a response.
Jun 06, 2015 4:36:18 PM org.apache.http.client.protocol.ResponseProcessCookies processCookies
WARNING: Invalid cookie header: "Set-Cookie: dweet:thing=hallowed-mailbox; path=/; expires=Sun,
05 Jun 2016 20:36:16 GMT; httponly". Invalid 'expires' attribute: Sun, 05 Jun 2016 20:36:16 GMT
Can someone please help me fix this frustrating error? I love Unirest, it's like requests but in Java too!
I encountered the same problem and found a solution:
RequestConfig globalConfig = RequestConfig.custom() .setCookieSpec(CookieSpecs.IGNORE_COOKIES).build();
HttpClient httpclient = HttpClients.custom().setDefaultRequestConfig(globalConfig).build(); Unirest.setHttpClient(httpclient);
copied from Kong/unirest-java#209
from @sachsgit :
Greetings,
My team would like to use Unirest for Java in our Software Testing, but Security is iffy on using "open source". They are worried about standards/quality.
Can anyone provide what standards/quality assurance that is used for Unirest for Java, so I can reassure Security that we can safely use Unirest for Java?
I tried to achieve request/response logging with simple decorator of HttpClient but I failed to do that because request/response InputStream implementation allows to read them only once. I am able to log headers and status code or uri but it would be nice to log body of both rq and rs.
I wonder if there is anything like this feature in plan or it can be achieved with current version.
Regards
Lukasz
Once Unirest.shutdown()
has been called there is no way to re-initialise Unirest because the new static Options.init()
method is private and that's the only one that can create the sync connection manager.
Use case: we use Unirest inside of AWS Lambda functions and therefore need to be able to call Unirest.shutdown()
for the Lambda process to exit cleanly. However, AWS may re-use what they call the Execution Context for the next invocation of the lambda, so we need to be able to re-initialise Unirest in the scenario that we shut it down but the same JVM is being used for a subsequent request.
Hi,
I'm using version 1.4.9, following code works fine:
HttpResponse<String> resp = Unirest.get("http://httpbin.org/get?a=1&b=2").asString();
System.out.println(resp.getBody());
and get:
{
"args": {
"a": "1",
"b": "2"
},
"headers": {
"Accept-Encoding": "gzip",
"Connection": "close",
"Host": "httpbin.org",
"User-Agent": "unirest-java/1.3.11"
},
"origin": "106.185.41.148",
"url": "http://httpbin.org/get?a=1&b=2"
}
but if I change url to http://httpbin.org/get?a=1&b=2#some_location
, I will get:
{
"args": {},
"headers": {
"Accept-Encoding": "gzip",
"Connection": "close",
"Host": "httpbin.org",
"User-Agent": "unirest-java/1.3.11"
},
"origin": "106.185.41.148",
"url": "http://httpbin.org/get"
}
I've also checked with wireshark and this problem do exist.
For further information this problem pertains to the block of code in HttpClientHelper:prepareRequest (lines 169-184)
package com.mashape.unirest.http;
import org.junit.Assert;
import org.junit.Test;
import java.net.URI;
import java.net.URL;
import java.net.URLDecoder;
import static org.junit.Assert.assertEquals;
public class HttpClientHelperManglerTest {
@Test
public void testMangler_encoding() throws Exception {
String s = "http://localhost/test%2Fthis";
assertEquals(s, urlMangler(s));
}
@Test
public void testMangler_fragment() throws Exception {
String s = "http://localhost/test?a=b#fragment";
assertEquals(s, urlMangler(s));
}
/*
* This function performs the logic as per (HttpClientHelper:169).
*/
private String urlMangler(String input) throws Exception {
String urlToRequest = null;
try {
URL url = new URL(input);
URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), URLDecoder
.decode(url.getPath(), "UTF-8"), "", url.getRef());
urlToRequest = uri.toURL().toString();
if (url.getQuery() != null && !url.getQuery().trim().equals("")) {
if (!urlToRequest.substring(urlToRequest.length() - 1).equals("?")) {
urlToRequest += "?";
}
urlToRequest += url.getQuery();
} else if (urlToRequest.substring(urlToRequest.length() - 1).equals("?")) {
urlToRequest = urlToRequest.substring(0, urlToRequest.length() - 1);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return urlToRequest;
}
}
The tested fragment is rewritten as http://localhost/test?#fragment?a=b
which thereby drops the query parameters
This same block of code also affects a bug with regards to encoding of reserved-characters (%2F -> /).
Is there any purpose to this block of code, the relevant new HttpXxx(urlToRequest) methods all validate the url and throw an IllegalArgumentException if invalid?
copied from: Kong/unirest-java#231
The current implementation doesn't account for any networking system settings (i.e. proxy settings). It's easy to fix by using the .useSystemProperties() method on an HttpClientBuilder.
Hi,
We are experiencing a hard-to-reproduce issue where OpenUnirest will throw an IllegalThreadStateException
when under heavy load.
We have a service which will at times invoke OpenUnirest at high frequency to deliver webhooks, and this is when we sometimes run into this issue, which doesn't seem recoverable unless we roll the app.
I submitted a few comments in an ancient issue on Kong/unirest-java about a similar issue with the HttpClientHelper
, but I don't think it was looked into, ever.
The partial stack trace is as follows:
java.base/java.lang.Thread.start(Thread.java:804)
io.github.openunirest.request.HttpClientHelper.asyncClient(HttpClientHelper.java:84)
io.github.openunirest.request.HttpClientHelper.requestAsync(HttpClientHelper.java:59)
io.github.openunirest.request.HttpClientHelper.requestAsync(HttpClientHelper.java:49)
io.github.openunirest.request.BaseRequest.asStringAsync(BaseRequest.java:64)
Environment:
java --version
: java 10.0.2 2018-07-17lsb_release -a
: Debian GNU/Linux 8.11 (jessie)cat /proc/cpuinfo
: Intel(R) Xeon(R) CPU E5-2690 v4I have no idea how
Hi,
first of all thanks for pushing unirest further. Unfortunately you still have the dependency the unfree org.json dependency. The problem is that the library org.json constains the line
The Software shall be used for Good, not Evil.
which makes it non-free and which means it' cant be used on debian or apache projects.
The author is not going to change the clause.
Further reading:
https://wiki.debian.org/qa.debian.org/jsonevil
https://lwn.net/Articles/707510/
stleary/JSON-java#331 (comment)
When I work with Spring RestTemplate class, I can use ParametrizedTypeReference instance to provide generic class to deserialization, like so:
ResponseEntity<ApiResponse<Payment>> response = template.exchange(request, new ParameterizedTypeReference<ApiResponse<Payment>>() {});
This is really nicely integrated in the overall code.
It would be nice to include a chapter on how to achieve this with Unirest. For example:
// Generic response type
HttpResponse response = Unirest.post(uri)
.header("Accept", "application/json")
.header("Content-Type", "application/json")
.body(body)
.asString();
ObjectMapper mapper = new ObjectMapper();
TypeReference<ApiResponse<Payment>> type = new TypeReference<ApiResponse<Payment>>() {};
ApiResponse<Payment> responseWrapper = mapper.readValue(response.getRawBody(), type);
Ideally, it would be nice to be able to provide some API where I can actually use type reference as a parameter, for example:
HttpResponse response = Unirest.post(uri)
.header("Accept", "application/json")
.header("Content-Type", "application/json")
.body(body)
.asObject(new TypeReference<ApiResponse<Payment>>() {});
Hello, I have problem with accesing this page, problem is with SSL certificate, exception message: javax.net.ssl.SSLException: hostname in certificate didn't match
If I open this page in browser (Google Chrome on Windows 10) there isn't any problem. Thanks for any help
I am getting an exception when downloading an file which is bigger than Integer.MAX_VALUE: Entity content is too long: 3272008971
Is there a workaround without overriding the HttpClient?
Currently redirects are followed silently and there does not seem to be an option for disabling automatic redirect following. When using unirest for testing, this is problematic.
Is there a way to disable automatic redirect following that I'm missing or can it be added?
A faster way than
Unirest.setHttpClient(org.apache.http.impl.client.HttpClients.custom()
.disableRedirectHandling()
.build())
When I use Unirest.config().setRequestHeader(name, value)
I expect Unirest to set the header on all requests. If the header is already there, I want to override it.
In previous Versions, this was the behavior. The new Version adds the same header again and again.
Is this behavior intended or is it a bug? And if it's intended, how do I override an existing header without clearing all other headers?
Sure, keep them as a map. But in the Headers.Entry, maybe have a field which is the order of addition.
Then with streams fu and #54, then maybe I can reconstruct the headers in the order in which they were specified on the server in question :)
Pro: denotes totally new ownership
Con: anyone wanting to upgrade will need to change namespace code
Vulnerabilities
DepShield reports that this application's usage of com.google.guava:guava:24.0-jre results in the following vulnerability(s):
This is an automated GitHub Issue created by Sonatype DepShield. Details on managing GitHub Apps, including DepShield, are available for personal and organization accounts. Please submit questions or feedback about DepShield to the Sonatype DepShield Community.
My Problem:
Now, I want to know the body. In Version 2, this was possible by using HttpResponse.getRawBody(). Unfortunately, now the response body is null and the raw body is an empty InputStream. Since the object mapper tries to map it to MyType.class, an exception gets thrown and the exception gets saved as HttpResponse.parsingError.
How do I access the message of the server? I could see the message if I override the ObjectMapper, catch the exception and extend the exception by the original body, but I don't think this is the intended way.
It seems that custom HttpClient was overridden by default HttpClient.
Hi,
I am just migrating from the mashable package and noticed that my async requests are not being processing correctly due to missing response content encoding handling. Digging into the ClientFactory, I noticed that something like
ab.addInterceptorLast(new RequestAcceptEncoding());
ab.addInterceptorLast(new ResponseContentEncoding());
is missing. Is this intended?
I noticed that libraries such as sardine (https://github.com/lookfirst/sardine) handle this with an enableCompression() function. (https://github.com/lookfirst/sardine/blob/d71beef1ffbf3df4095f5f59e5d46ceabb904191/src/main/java/com/github/sardine/impl/SardineImpl.java#L290)
Should I set my own httpasyncclient if I want content encoding? Or do you plan to update the client factory to support this? Any advise is appreciated.
Robert
This could be injected over the Apache client and would support mocked internal calls for testing similar to feignmock https://github.com/velo/feign-mock
(in fact, maybe even support feign-mock)
.asJson()
cannot be resolved.
Anybody know why?
OK, so Subversion has a bunch of HTTP 1.1 verbs/methods that are not in the set you support (GET, POST, PUT, DELETE, PATCH, HEAD & OPTIONS).
Subversion also has: PROPFIND, REPORT, MKACTIVITY, PROPPATCH, CHECKOUT, MKCOL, MOVE, COPY, LOCK, UNLOCK, MERGE.
There's a case for a open spec'd name-your-own verb, but I'd be happy with the above.
Hello!
I was linked this work, which seems to be very active recently. I am happy that someone is still putting work into this!
Can you possibly summarise what changes were made in this fork in terms of bugs fixed and new features? Thanks!
This is a library for the future!!! Old Java holds it back from greatness
copied from Kong/unirest-java#233
from @Alcuin-van-Zijl
The static function refresh is called every time the Options are invoked statically.
On line 72 you create a new PoolingHttpClientConnectionManager.
Online 77 you set a new option with the new PoolingHttpClientConnectionManager.
copied from Kong/unirest-java#233
The previous one (in the options map) if there was one is now not linked to anything.
As the PoolingHttpClientConnectionManager is a thread the garbage collector will not be removing the old manager.
Also this setup provides a performance hit. The PoolingHttpClientConnectionManager is build around the idea that you can reuse connections. Creating a new one for every call is not very efficient.Note that this issue is not very noticeable unless you do a lot of calls or have very little memory available.
As the leak are threads they are not stored in the reserved memory so setting the max heap will not prevent this leak. I noticed this leak when my server got more virtual memory used than there was real memory. The server then started swapping the memory. As they are threads the swapping continued endlessly as the server needed to check the status of the thread every now and then.
Using the memory analyzer of eclipse its pretty apparent what the issue is. Do some 100 calls and you will end up with 100 PoolingHttpClientConnectionManager threads.The same issue exists for the async manager but as I do not use this one I have not investigated this any further.
Maybe should introduce the term: Thread leak for this instead of memory leak :)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.