java-http-client | SendGrid 's Java HTTP Client for calling APIs | REST library
kandi X-RAY | java-http-client Summary
kandi X-RAY | java-http-client Summary
If you are looking for the SendGrid API client library, please see this repo.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
- Main method of the SendGrid API
- Add the query parameters to a URL
- Wraps the given request
- Performs an HTTP POST request
- Resets the object
- Clear the base uri
- Clears the endpoint
- Clear the headers
- Handles an entity response
- Convert entity to string
- Closes the HTTP client
- Removes the given header
- Removes a query param
java-http-client Key Features
java-http-client Examples and Code Snippets
Client client = new Client();
Request request = new Request();
request.setBaseUri("api.test.com");
request.setMethod(Method.GET);
String param = "param";
request.setEndpoint("/your/api/" + param + "/call");
try {
Response response = client.api(
...
dependencies {
...
compile 'com.sendgrid:java-http-client:4.3.9'
}
repositories {
mavenCentral()
}
...
Community Discussions
Trending Discussions on java-http-client
QUESTION
I want to use the new java.net.HttpClient to do some requests to another system.
For debug purposes I want to log (and later store in our db) the request that I send and the response that I receive.
How can I retrieve the effective http headers, that java is sending?
I tried to get the headers like this:
...ANSWER
Answered 2021-May-17 at 13:38I have an unfortunate answer to your question: Regrettably, impossible.
Some background on why this is the case:
The actual implementation of HttpRequest used by your average OpenJDK-based java-core-library implementation is not java.net.http.HttpRequest
- that is merely an interface. It's jdk.internal.net.http.HttpRequestImpl
.
This code has 2 separate lists of headers to send; one is the 'user headers' and the other is the 'system headers'. Your .headers()
call retrieves solely the user headers, which are headers you explicitly asked to send, and, naturally, as you asked for none to send, it is empty.
The system headers is where those 6 headers are coming from. I don't think there is a way to get at these in a supported fashion. If you want to dip into unsupported strategies (Where you write code that queries internal state and is thus has no guarantee to work on other JVM implementations, or a future version of a basic JVM implementation), it's still quite difficult, unfortunately! Some basic reflection isn't going to get the job done here. It's the worst news imaginable:
These 6 headers just aren't set, at all, until
send
is invoked. For example, the three headers that are HTTP2 related are set in the package-privatesetH2Upgrade
method, and this method is passed the HttpClient object, which proves that this cannot possibly be called except in the chain of events started when you invokesend
. An HttpClient object doesn't exist in the chain of code that makes HttpRequest objects, which proves this.To make matters considerably worse, the default HttpClient impl will first clone your HttpRequest, then does a bunch of ops on this clone (including adding those system headers), and then sends the clone, which means the HttpRequest object you have doesn't have any of these headers. Not even after the send call completes. So even if you are okay with fetching these headers after the send and are okay with using reflecting to dig into internal state to get em, it won't work.
You also can't reflect into the client because the relevant state (the clone of your httprequest object) isn't in a field, it's in a local variable, and reflection can't get you those.
A HttpRequest can be configured with custom proxies, which isn't much of a solution either: That's TCP/IP level proxies, not HTTP proxies, and headers are sent encrypted with HTTPS. Thus, writing code that (ab)uses the proxy settings so that you can make a 'proxy' that just bounces the connection around your own code first before sending it out, in order to see the headers in transit, is decidedly non-trivial.
The only solution I can offer you is to ditch java.net.http.HttpClient
entirely and use a non-java-lib-core library that does do what you want. perhaps OkHttp. (Before you sing hallelujah, I don't actually know if OkHttp can provide you with all the headers it intends to send, or give you a way to register a hook that is duly notified, so investigate that first!)
QUESTION
We want to migrate all our apache-httpclient-4.x code to java-http-client code to reduce dependencies. While migrating them, i ran into the following issue under java 11:
How to set the socket timeout in Java HTTP Client?
With apache-httpclient-4.x we can set the connection timeout and the socket timeout like this:
...ANSWER
Answered 2020-Oct-27 at 09:04You can specify it at the HttpRequest.Builder
level via the timeout method:
QUESTION
I struggled with this problem for quite some time; and after finding a simple solution... wanted to ask a question & answer!!
The question has been asked in different ways multiple times on stack overflow, and the accepted solutions
are either partially correct and complex
or talk about response
compression.
Aggregating some old Q&A on this topic:
wrong accepted ans/partially correct & complex subsequent ans.
- HTTP request compression
- How to decode Gzip compressed request body in Spring MVC: (Almost correct 2nd ans, but complex overkill)
The accepted ans is wrong. It's about
RESPONSE
compression and notREQUEST
.Similar Questions - terminated at "NO request compression"
A specific question & ans for Spring RestTemplate framework: How to zip- compress HTTP request with Spring RestTemplate?
ANSWER
Answered 2020-Jul-07 at 21:46A simple solution is by using a filter. (See servlet-filter tutorial)
Create a Servlet Filter:
- Make sure that the filter is called either first/before any filters which use request body.
I. Register filter in web.xml:
QUESTION
I'm using the new java.net.http.HttpClient
and would like to change the user agent string. By default it sends Java-http-client/11.0.6
but I'd specify some string on my own.
Any idea how to do this?
...ANSWER
Answered 2020-Mar-25 at 17:48There was an existing bug, it's resolved now.
QUESTION
We are using JDK11 java.net.http
HTTP client to get data from an API. After we receive the response the connections remain opened in our server with TCP state CLOSE_WAIT
, which means the client must close the connection.
From RFC 793 terminology:
CLOSE-WAIT - represents waiting for a connection termination request from the local user.
This is our client code which runs on WildFly 16 running on Java 12 as a stateless REST API. We don't understand why this is happening.
...ANSWER
Answered 2019-Mar-22 at 12:00I wouldn't recommend creating a new client for every new request. This is defeating the purpose of HTTP/2 which allows multiplexing requests on a single connection.
The second thing is that the two properties:
QUESTION
I've added an interceptor to my JAX-RS / Resteasy Java SDK13 project running on Wildfly 18 in order to use annotations (e.g. @RolesAllowed). While the security implementation is way better than the programmatic approach, I'm getting an invocation error when Resteasy tries to call the matched function (deleteAll()). I've traced through the Interceptor flow just prior to the attempted invocation and the Interceptor approves the user and passes control onward. Then I get this error, which seems to be a security failure, despite my interceptor having approved the user.
I changed the Wildfly setting
but this hasn't changed the behavior.
Java Source being called with /consumers/deleteall
with nothing in the body and no query string.
Security Interceptor
...ANSWER
Answered 2020-Jan-14 at 07:40You are using an EJB (@LocalBean
) annotation. And you applied @RolesAllowed
annotation. This means, your EJB / REST service invocation is automatically protected by the JEE runtime.
Basically, what you are doing in your interceptor is already performed by Wildfly. But in a different way. Now you accidentally have two ways or levels of authentication. I suggest you stick to a single implementation.
Either you go with JAAS framework or use your custom interceptor. You should not use both at the same time.
- My recommended approach would be to setup a JAAS Login module in Wildfly. There should be plenty of examples of how to do that, e.g. use https://docs.wildfly.org/18/WildFly_Elytron_Security.html#Database_Authentication_Migration or JBoss Wildfly - database login module
- Do not use JAAS and provide your custom security interceptor. Basically you could keep your interceptor class. In order to make it work, you could use your own set of annotations to check for access.
QUESTION
Here's my code:
...ANSWER
Answered 2018-Dec-30 at 16:27The behavior from the Java11 client code seems correct. The Host section elaborates on the details. By the way, from the documentation of HttpRequest
builder header(String name, String value)
:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install java-http-client
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page