logo
down
shadow

How do i get the status code for a response i subscribe to using the JDK's HttpClient?


How do i get the status code for a response i subscribe to using the JDK's HttpClient?

By : littlerain
Date : October 25 2020, 07:10 AM
I hope this helps . As noted in the documentation, these BodyHandlers
code :
// a subscriber which expresses a complete lack of interest in the body
private static class Unsubscriber implements HttpResponse.BodySubscriber<Void> {
    @Override
    public CompletionStage<Void> getBody() {
        return CompletableFuture.completedStage(null);
    }

    @Override
    public void onSubscribe(Flow.Subscription subscription) {
        subscription.cancel();
    }

    @Override
    public void onNext(List<ByteBuffer> item) {}

    @Override
    public void onError(Throwable throwable) {}

    @Override
    public void onComplete() {}
}


// wraps another handler, and only uses it for an expected status
private static HttpResponse.BodyHandler<Void> expectingStatus(int expected, HttpResponse.BodyHandler<Void> handler) {
    return responseInfo -> responseInfo.statusCode() == expected ? handler.apply(responseInfo) : new Unsubscriber();
}

// used like this
Flow.Subscriber<String> subscriber = createSubscriberSomehow();
HttpResponse<Void> response = HttpClient.newHttpClient()
                                        .send(HttpRequest.newBuilder()
                                                         .uri(URI.create("http://example.org/api"))
                                                         .build(),
                                              expectingStatus(200, HttpResponse.BodyHandlers.fromLineSubscriber(subscriber)));


Share : facebook icon twitter icon
HttpClient getting status codes and follow redirects to get end page status code

HttpClient getting status codes and follow redirects to get end page status code


By : lihtaryk
Date : March 29 2020, 07:55 AM
may help you . The issue is that you are sending a HEAD request instead of a GET request, for which the xbox site just return a 404.
Using:
code :
HttpGet test = new HttpGet("http://support.xbox.com/en-US/xbox-live/troubleshoot/game-play");
How to get HttpClient returning status code and response body?

How to get HttpClient returning status code and response body?


By : fan he
Date : March 29 2020, 07:55 AM
this one helps. Don't provide the handler to execute.
Get the HttpResponse object, use the handler to get the body and get the status code from it directly
code :
HttpResponse response = client.execute(httpGet);
String body = handler.handleResponse(response);
int code = response.getStatusLine().getStatusCode();
Response Status code 400:Bad Request because of posting data with large size (httpclient)

Response Status code 400:Bad Request because of posting data with large size (httpclient)


By : Kathrin
Date : March 29 2020, 07:55 AM
Any of those help As this below links, the answer is : if you want to send a file which have the size larger than 2GB via POST you must be use splitting and then send the pieces of file. https://forums.asp.net/t/2124827.aspx?Response+Status+code+400+Bad+Request+because+of+posting+data+with+large+size+httpclient+
and
httpclient response is null when API returns a 204 status code

httpclient response is null when API returns a 204 status code


By : user2158959
Date : March 29 2020, 07:55 AM
this will help First of all, I would say it is strange (or wrong) that your backend is returning a 204 for an empty collection instead of a 200 together with an empty list.
Using Angular7 and RxJS6 I constructed a test case where my backend is configured as follows:
code :
[HttpGet("/Products/latest")]
public async Task<NoContentResult> list()
{
  return NoContent();
}
public getFromAPI() {
  return this.httpClient.get('/Products/latest', { observe: 'response' }).pipe(
    switchMap(res => res.status === 204 ? of([]) : of(res))
  ).subscribe();
}
Which response status code goes to catchError in Angular 6+ httpClient?

Which response status code goes to catchError in Angular 6+ httpClient?


By : user2586229
Date : March 29 2020, 07:55 AM
Does that help 4xx and 5xx status codes are errors. The other ones are success.
http://angular.io/guide/http#getting-error-details
Related Posts Related Posts :
  • Java - JPA - Generators - @SequenceGenerator
  • What's a good Java-based Master-Slave communication mechanism?
  • Design Help/Suggestion required
  • NullPointerException in com.sun.tools.jxc.SchemaGenTask
  • time required to finish the multithreaded program?
  • Traditional IO vs memory-mapped
  • Using StringBuilder to process csv files to save heap space
  • Response Time is different for mulitiple execution of the application with the same request Performance problem
  • Java - Weblogic - JMX : request available free memory on all servers
  • Error: Can't find common super class of
  • JNI problem when calling a native library that loads another native library
  • calling Stanford POS Tagger maxentTagger from java program
  • Notification framework for java
  • hide keyboard in android?
  • Alternative of JPA
  • How to get Java XP Look and Feel
  • Remote JMS connection still using localhost
  • Java NoClassDefFoundError
  • Is O(N!*N) an acceptable big oh complexity class or do I remove the constant and just say O(N!)?
  • Java File URI error?
  • Using an interface as a constructor parameter in Java?
  • Add additional path to exec-maven-plugin
  • Natural Language parsing of an appointment?
  • Java: How to tell which fontconfig file my system is currently using?
  • How to create notification icon badge on Android apps (like iPhone)
  • Interfaces with hibernate annotations
  • Getting ConnectionTimeoutException with the host did not accept the connection within timeout
  • How to get all property names of a Groovy class?
  • Java Netscape LDAP Remove One Attribute
  • A simple two column layout with Swing
  • Making a Linked list using Generics
  • how to handle result set data
  • Cloning java ArrayList and preventing it from modifications
  • Java How to call method of grand parents?
  • running jython bytecode using java
  • get content from website with utf8 format
  • Sendgrid v3: "Substitutions may not be used with dynamic templating"
  • Application freezes when running method
  • Maven versioning using git branches
  • does a Java getter incur a performance penalty
  • Open Source Implementation of Checkers
  • How can I make a resizable array in Java?
  • Change size of ImageIcon in a JRadioButton
  • Java memory mapped files and swap
  • JAXB Annotated class - setting of a variable which is not an element
  • JSF don't find component in view root with the form id
  • Using Mockito, how do I match against the key-value pair of a map?
  • How do I run my application as superuser from Eclipse?
  • Spring-Security: Call method after authentication
  • How can I add a password to this JDBC:ODBC connection string that is trying to connect to an MS Access database
  • java.sql.SQLException: database locked
  • Json Jackson deserialization without inner classes
  • Error starting modern compiler
  • Generate a random number in interval [0,360) which is divisible by number 15
  • Rotate watermark text at 45 degree angle across the center Apache PDFBox
  • Generate Random number in a range with no repeatation
  • Starting HBASE, java.lang.ClassNotFoundException: org.apache.htrace.SamplerBuilder
  • Using getter and setter methods inside a class in my main class
  • java: <identifier> expected with ArrayList
  • Java Reflection, java.lang.IllegalAccessException Error
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk