-3

beans.scala - class contains connection to server

 lazy val ConnectionFlow: Flow[HttpRequest, HttpResponse, Any] =
    Http().outgoingConnection(config.getString("host"), config.getInt("port"))

  lazy val AppService = new Service(config, ConnectionFlow)

Service.scala class

def Request(request: HttpRequest): Future[HttpResponse] =
    Source.single(request).via(ConnectionFlow).runWith(Sink.head)

//building Json request 
val reqJs = Json.obj("PARAMS" -> Json.obj("param1" -> value1))

Request(RequestBuilding.Post("/services/serviceName",reqJS).flatMap { response =>
// need response to be in JSobject format but the service returns application/xml format 
yiksanchan
  • 1,890
  • 1
  • 13
  • 37
  • Welcome to Stack Overflow. Please take the [tour](https://stackoverflow.com/tour), read about [what's on-topic](https://stackoverflow.com/help/on-topic), and read [How to Ask a Good Question](https://stackoverflow.com/help/how-to-ask). – Can you clarify what your question or problem is? Or is this supposed to be an answer to a problem you had? – Ivo Mori Jul 28 '20 at 00:16
  • Need to add header(accept-> "application/json") to the post request while building it but the service is returning application/xml format of response . – user2194771 Jul 28 '20 at 04:52
  • thanks for the guidance, this is very new to me. i'll go through them – user2194771 Jul 28 '20 at 04:56

1 Answers1

0

If I understand correctly, you're asking how to modify the request s.t. it indicates to the server it expects a JSON response.

To do that, you can try

val builder = RequestBuilding.Post("/services/serviceName",reqJS)
builder.addHeader(Accept(MediaRange(`application/json`)))
// send out the request as you did

According to https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept, Accept Header only advertisers which content types the client is able to understand, while it is fully on the server's mercy to respect this request. That being said, the server could literally respond with anything they want, despite the header you send.

yiksanchan
  • 1,890
  • 1
  • 13
  • 37