I am trying to create HttpRequest and try to submit json to endPoint , My rest services are written using akka-http
case class JobInfo(jobName: String,
appId: Option[String] = None,
status: Option[String] = None,
modify_date: Option[Long] = None)
object Test extends App{
import HttpMethods._
val fieldList = List(
("jobName",Json.fromString("TestJobA")),
("appId",Json.fromString("1234")),
("status",Json.fromString("Running")),
("modify_date",Json.fromLong(DateTime.now.getMillis))
)
val json = Json.fromFields(fieldList)
val endPoint = Uri.from(scheme = "http",
host = "0.0.0.0",
port = 7000,
path = s"/updateJobDetails/").toString()
val requestEntity = HttpEntity(MediaTypes.`application/json`, json.toString)
// val postRequest1 = HttpRequest.POST("/receive").withEntity(requestEntity)
HttpRequest(POST,"http://0.0.0.0:7000/updateJobDetails",entity = requestEntity)
}
But this doesnot seem to work, Earlier I used spray to do the same as
val pipe: HttpRequest => Future[HttpResponse] = sendReceive
val json =
s"""{
| "jobName" : "jobA",
| "appId" :"${jobInfo.appId.getOrElse("Not Set")}",
| "status" :"${jobInfo.status.getOrElse("Not Set")}",
| "modify_date" :${DateTime.now.getMillis}
|}""".stripMargin.parseJson.asJsObject()
//Rest EndPoint for JobDetails updation.
val endPoint = Uri.from(scheme = "http",
host ="0.0.0.0",
port = 7000,
path = s"/updateJobDetails").toString()
val response = pipe(Put(endPoint, json))
Can anyone help me correcting the what I am missing
When you say "not working" also provide the error message you got.
Anyway, from the code you have posted above. you are building a future but you are not waiting for it to complete.
The code below shows how to build a Future and then wait for it to complete.
object Client {
def main(args: Array[String]): Unit = {
implicit val system = ActorSystem()
implicit val materializer = ActorMaterializer()
// needed for the future flatMap/onComplete in the end
implicit val executionContext = system.dispatcher
val responseFuture: Future[HttpResponse] = Http().singleRequest(HttpRequest(uri = "http://akka.io"))
responseFuture
.onComplete {
case Success(res) => println(res)
case Failure(_) => sys.error("something wrong")
}
Await.result(responseFuture, Duration.Inf)
}
}