Skip to content
Advertisement

Send a simple POST request from Quarkus/Java

I want to send a simple POST request to another application to trigger some action there.

I have a quarkus project and want to send the request from inside my CreateEntryHandler – is this possible in a simple way? Or do I need to add something like Apache Httpclient to my project? Does it make sense in combination with quarkus?

Advertisement

Answer

The other application, I assume has an API Endpoint?

Lets state that the API endpoint you are trying to call in the other app is:

POST /v1/helloworld

From your Quarkus Application, you will have to do the following:

  • Register a RestClient *As a Service
  • Specify the Service information in your configuration properties
  • Inject and use this Service

— In your current Application —

Pay close attention to the package name. IT has to match exactly in your application.properties file.

HelloWorldService.java

JavaScript

//Notice that we are not including the /v1 in the mp-rest/url, why? Because it is included in the @RestClient path.

Update your application.properties to include the following:

JavaScript

— Your HelloWorldPojo which you will send as payload

HelloWorldProjo.java

JavaScript

In another service where you actually want to use this:

ServiceWhichCallsYourOtherAPI.java

JavaScript

The POST request will then go to https://yourOtherApplication.com/API/v1/helloworld

The json will look like:

JavaScript

Really great read: https://quarkus.io/guides/rest-client

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement