0

This is the code in scala how can we do a similar thing in java using the play framework reference: -> Play WS - post multipart/form-data from scala - Bad Request

import akka.stream.scaladsl.FileIO
    import akka.stream.scaladsl.Source
    import play.api.libs.ws.WSClient
    import play.api.mvc.MultipartFormData._
    
    val pathToFile = "./sampleCV.pdf"
    val fileName = "sampleCV.pdf"
    val futureResponse = ws.url(url).withRequestTimeout(Duration.create(55, TimeUnit.SECONDS))
          .addHttpHeaders("authorization" -> s"bearer $access_token")
          .addHttpHeaders("accept" -> "*/*")
          .addHttpHeaders("content-type" -> "multipart/form-data")
          .post(Source(
            FilePart("File", fileName, Option("application/pdf"), FileIO.fromPath(Paths.get(pathToFile)))  :: List()
          ))

I had tried but got 412 as the response code as I was using an HTTP multipart file.

suv1sh
  • 43
  • 1
  • 10
  • how to do this scala code into java: ws.url(url).post(Source(FilePart("hello", "hello.txt", Option("text/plain"), FileIO.fromFile(tmpFile)) :: DataPart("key", "value") :: List())) reference -> https://www.playframework.com/documentation/2.5.x/ScalaWS – suv1sh Dec 15 '22 at 15:55
  • i am getting :- after tring this approach :-> https://www.playframework.com/documentation/2.5.x/JavaWS Hi, I'm trying to retrieve the JSON response from my API but I'm having a problem if the status code is 412. It just keeps on loading and no response is being returned. Any reason for this? – suv1sh Dec 15 '22 at 16:34
  • it worked for me -> playframework.com/documentation/2.5.x/JavaWS I was passing the wrong headers authenticated and URL combination without HTTPS which worked after providing hostname with HTTPS – suv1sh Dec 16 '22 at 07:40

0 Answers0