Home > Net >  Java Akka actor's HTTP POST request not hitting the post path
Java Akka actor's HTTP POST request not hitting the post path

Time:11-16

I am creating a post request using akka version 2.6.17 and akka http 10.2.7. My system is bound to port 8080 and can receive post requests from Postman just fine. However, when sending a post request from within akka itself (an actor sending a post request), the POST path is never hit. Here is the post request

public void postStockCheck(){
        String data = "{\"id\": \"yes\"}";
        HttpRequest.POST("http://localhost:8080/hello")
        .withEntity(HttpEntities.create(ContentTypes.TEXT_PLAIN_UTF8, data));
    }

Here is the path:

return route(
        path("hello", () ->
            post(() ->
                entity(Unmarshaller.entityToString(), (string) -> {
                  System.out.println("request body: "   string);
                  return complete("Success");
                })
              )));
      }

As mentioned, the path will work from postman. If I'm missing something, any help is appreciated!

CodePudding user response:

In postStockCheck you have created a HttpRequest, but haven't fired it. To fire the request you could use singleRequest method from Http.

public void postStockCheck(ActorSystem system) {
    String data = "{\"id\": \"yes\"}";

    Http.get(system)
            .singleRequest(HttpRequest.POST("http://localhost:8080/hello")
            .withEntity(HttpEntities.create(ContentTypes.TEXT_PLAIN_UTF8, data)));
}

To get a better picture of firing the request and collecting responses, go through the docs.

  • Related