How to replace the header value

  def seqServiceRoute: Route = path("api" / "v1" / "sequences" / "commodities" ) {
post {
  entity(as[String]) { reqData =>
    extractRequest { req =>
      log.debug(s"received sbu request, ${req.method} ${req.uri}   ${req.headers} ${req.entity}")
      req.headers.map(x => log.debug(x.name()))
      val hdrs = req.headers.filter(header => (header.isNot("timeout-access") && header.isNot("host")))
      hdrs.map(x => log.debug(x.name()))
      hdrs.map(x => x.name() match
      {
        case "X-nodeId" => x.value().replace("30001","5585")
        case _ => x
      })
      val seqServiceUri = s"${seqHost}/api/v1/sequences/commodities"
      val responseFuture = http
        .singleRequest(
          request = HttpRequest(method= HttpMethods.POST, uri = Uri(seqServiceUri).withRawQueryString(req.uri.rawQueryString.getOrElse("")),
            headers = hdrs,
            entity = HttpEntity(ContentTypes.`application/json`,reqData)),
          connectionContext = httpsCtx)
        .flatMap(response => response.entity.dataBytes.runReduce(_ ++ _))
      onComplete(responseFuture) {
        case Success(resp) => complete {
          log.debug("response is " + resp)
          HttpEntity(ContentTypes.`application/json`, resp)
        }
        case Failure(ex) => complete {
          ex.printStackTrace()
          log.error(ex, ex.getMessage)
          (StatusCodes.InternalServerError, s"An error occurred: ${ex.getMessage}")
        }
      }
    }
  }
}

HI Team, I am trying to replace one of the header value but it is not working. I tried the statement.

case “X-nodeId” => x.value().replace(“30001”,“5585”)

But it seems not working in the HTTPEntity. Can someone please help?

You are saying hdrs.map(...) to construct a new value (twice in fact) but you’re never using the result so it’s discarded. Perhaps you want something like

val hdrs = req.headers
  .filter(header => (header.isNot("timeout-access") && header.isNot("host")))
  .map(x => log.debug(x.name()))
  .map(x => x.name() match  {
    case "X-nodeId" => x.value().replace("30001","5585")
    case _ => x
  })

Hi. one of my header value is “X-nodeId:30001” and I wanted to replace that to “X-nodeId:5585” and run the single request.

I tried the above suggestion and I am getting this error while passing the header.

val responseFuture = http .singleRequest( request = HttpRequest(method= HttpMethods.POST, uri = Uri(seqServiceUri).withRawQueryString(req.uri.rawQueryString.getOrElse("")), headers = hdrs, entity = HttpEntity(ContentTypes.application/json,reqData)), connectionContext = httpsCtx) .flatMap(response => response.entity.dataBytes.runReduce(_ ++ _))

Requrired.HTTPHeader but found Object. How do I pass the “hdrs”?

1 Like

Actually this should be a separate statement, I didn’t even look at the body of the map call. You also need to turn the result you’re computing back into a header (see comment below)

val hdrs0 = req.headers
  .filter(header => (header.isNot("timeout-access") && header.isNot("host")))

hdrs0.foreach(x => log.debug(x.name()))

val hdrs = hdrs0
  .map(x => x.name() match  {
    case "X-nodeId" => x.value().replace("30001","5585") // but you need to turn this into a header! It's probably a string at this point.
    case _ => x
  })

I think there’s enough confusion here that you might want to ask on a more immediate medium, like Gitter. But I suggest you slow down and follow the types and be sure you understand what’s going on.

Yeah that is where I Am stuck too. I do not need that debug part. I wanted to change the header value. How to convert the String to HttpHeader? can you help