I'm implementing an external API where I need to send a file attachment bundled with a JSON meta part.
The following code is not accepted by the server since Play hardcodes the content type of DataPart
to text/plain
, and the server expects application/json
val meta = Json.obj(
"name" -> s"Invoice ${invoiceNumber}.pdf",
"referenceType" -> "INVOICE",
"referenceId" -> 42
)
ws.url("{API-URL}")
.addHttpHeaders("Authorization" -> s"Bearer ${accessToken}")
.post(Source(DataPart("meta", meta.toString) :: FilePart("file", s"Invoice ${invoiceNumber}.pdf", Option("application/pdf"), FileIO.fromPath(file.toPath)) :: List()))
.map(res => {
logger.debug("Status: " + res.status)
logger.debug("JSON: " + res.json)
Right(invoiceNumber)
})
The example curl (that I've tested and verified) command for the API endpoint is:
curl -H "Authorization: Bearer {accessToken}" \
-F 'meta={"name": "Invoive 4.pdf", "referenceType": "INVOICE", "referenceId": 42 } \
;type=application/json' \
-F "file=@Invoice.pdf" \
'{API-URL}'
Is there a simple way to either force DataPart
to use a different content-type or use a different Part to get more control over what I'm sending?