Giter Club home page Giter Club logo

unirest-java's Introduction

This project went home to the mothership

This project has been merged with https://github.com/Kong/unirest-java. Please see that project as the primary place for new features and security issues.

unirest-java's People

Contributors

abhinav-upadhyay avatar bludwarf avatar brothhaar avatar dewos avatar esseguin avatar hennr avatar iximeow avatar jdbevan avatar jgalo80 avatar jghoman avatar kickroot avatar marcelocyreno avatar mchiareli avatar nijikokun avatar orlingueorguievivu avatar pilif avatar ryber avatar shashiranjan84 avatar shatsar avatar sonicaghi avatar stevespringett avatar subnetmarco avatar tcoxon avatar varra4u avatar xxrockonxx avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

unirest-java's Issues

Make Verify Actually Work

It doesn't now because it it makes external web service calls to things that don't work like that anymore

Question about multi-threading and multiple REST servers

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

OutOfMemoryError on large file download on asBinary

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?

Support for Java 8 CompletableFuture

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"));
    }
}

HttpResponse not consume responseEntity if objectMapper throw exception

from Kong/unirest-java#114

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

Add charset to MultipartBody

from Kong/unirest-java#206

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);
...

QueryString null

from Kong/unirest-java#193

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

Headers has size() but no way to enumerate over them

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.

Invalid Headers when using Unirest

per Kong/unirest-java#97

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);

[Query] Coding Standard/Quality Used #209

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?

Add request/response logging capabilities.

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

Unirest.refresh() should re-create sync monitor

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.

Request losts url query parameters if url contains fragment

from Kong/unirest-java#207

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?

IllegalThreadStateException under high load

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:

  • OpenUnirest version: 2.4.02
  • java --version: java 10.0.2 2018-07-17
  • lsb_release -a: Debian GNU/Linux 8.11 (jessie)
  • cat /proc/cpuinfo: Intel(R) Xeon(R) CPU E5-2690 v4

Switch to other json lib (openjson) instead of org.json due to licence clause in original

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)

Support Type References For DeSerialization

from Kong/unirest-java#200

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>>() {});

SSL - Server Name Indication problem

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

ContentTooLongException

Kong/unirest-java#211

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?

Add Option to never follow redirects

From Kong/unirest-java#109

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())

Unirest.config().setRequestHeader adds header instead of setting it

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?

Headers in original order, please

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 :)

[DepShield] (CVSS 5.9) Vulnerability due to usage of com.google.guava:guava:24.0-jre

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.

How to get Response Body on server error

My Problem:

  • I want to get an object from a server. The type is MyType.class.
  • The server throws an exception. It sends a message of the exception in the Http Body.
  • The body is not of type MyType.class

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.

ClientFactory does not create a CloseableHttpAsyncClient with Encoding support

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

Support for weird HTTP verbs, please

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.

Differences with the Kong Unirest

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!

Memory leak in: Options.java

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 :)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.