9

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?

RNH
  • 239
  • 1
  • 3
  • 13

1 Answers1

19

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

package com.helloworld.services

@Path("/v1")
@RegisterRestClient
public interface HelloWorldService{

    @POST
    @Path("/helloworld")
    Response callHeloWorld(HelloWorldPojo payloadToSend);
}

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

com.helloworld.services.HelloWorldService/mp-rest/url=https://yourOtherApplication.com/API 

--- Your HelloWorldPojo which you will send as payload

HelloWorldProjo.java

@JsonPropertyOrder({"id", "name"})
public class HelloWorldProjo{

  private long id;
  private String name;

  //Setters
  //Getters

}

In another service where you actually want to use this:

ServiceWhichCallsYourOtherAPI.java

@RequestScoped
public class ServiceWhichCallsYourOtherAPI{


    @Inject
    @RestClient
    HelloWorldService helloWorldService;



    public void methodA(){

         HelloWorldPojo payloadToSend = new HelloWorldPojo();
         payloadToSend.setId(123);
         payloadToSend.setName("whee");

         helloWorldService.callHelloWorld(payloadToSend);

   } 

}

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

The json will look like:

{
  "id":123,
  "name":"whee"
}

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

JCompetence
  • 6,997
  • 3
  • 19
  • 26
  • Hey buddy, first of all thank you!! This one really helps me out. To be honest I am quite noob in quarkus - @RegisterRestClient seems not to be recognized, what package do I need? I added org.eclipse.microprofile.rest.client to my pom.xml but it doesn't seem to help. Any idea? Thanks again :) – RNH Feb 24 '21 at 12:12
  • io.quarkus quarkus-rest-client io.quarkus quarkus-rest-client-jackson – JCompetence Feb 24 '21 at 12:14
  • Fixed! If you do not mind: How can I add some simple JSON to this POST request? [Sorry for chatting here] – RNH Feb 24 '21 at 12:18
  • I updated the method to take in a Payload Object. Simple one. Hope that helps you. – JCompetence Feb 24 '21 at 12:27
  • Do you mind adding in which files the code snippets belong? I was confused about the HelloWorldPojo. Besides that huge THANK YOU buddy!! – RNH Feb 24 '21 at 13:54
  • done @RNH Do update me if you get it working :), – JCompetence Feb 24 '21 at 14:19
  • 1
    Sorry for the late reply - nice update. Worked like a charm! Thanks a lot my friend :) – RNH Mar 08 '21 at 11:39
  • What if the URL is unknown? you load the URL from some object to to send request, assuming all the URLs accept the same request format? how to make the rest client accept a specific URL every time? – Mr.Cat Sep 21 '21 at 13:15
  • 1
    @Mr.Cat You could use RestClientBuilder See https://wordpress.com/post/jcompetence.com/598 https://github.com/smustafa/quarkus-dynamic-microprofile-path – JCompetence Sep 27 '21 at 10:57
  • https://jcompetence.com/2021/09/27/microprofile-dynamic-baseuri-rest-client/ – JCompetence Apr 04 '22 at 14:54
  • If this is simple... – igr Sep 30 '22 at 11:05
  • The property should now be:   `quarkus.rest-client."com.helloworld.services.HelloWorldService".url="https://yourOtherApplication.com/API"` – Anonymous404 Oct 28 '22 at 12:43