Search code examples
javahttpserver

How to make POST request with HttpServer?


I have created a local HTTP server (using Java’s HttpServer class) to redirect end-users after authenticating themselves to an authorization server using OAuth 2.0. Is it possible to send a POST request from this server to the authorization server through HttpServer (to continue with the OAuth process)? If not, how should I send a POST request from this server to the authorization server?

The Java documentation seems to suggest that the HTTP Server can only respond to requests and not send any requests itself.

Thanks in advance.


Solution

  • Correct, the HttpServer class can only handle (client) requests and respond to them.

    A HttpServer is bound to an IP address and port number and listens for incoming TCP connections from clients on this address

    If you want to POST a request to another service, you'll need to implement a client to do so using something like HttpClient.