Posting a file to an API from a workflow process doesn't work | Community
Skip to main content
Level 2
August 23, 2024
Solved

Posting a file to an API from a workflow process doesn't work

  • August 23, 2024
  • 1 reply
  • 346 views

I have the following logic in a WorkflowProcess:

String addFileUrl = _apiUrl + "File/AddFileToItem"; HttpPost post = new HttpPost(addFileUrl); post.setHeader(HttpHeaders.AUTHORIZATION, "Bearer " + _accessToken); // Build the multi-part request entity MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.addTextBody("itemNumber", itemNumber); if(revision != null && StringUtils.isNotEmpty(revision)) builder.addTextBody("revision", revision); builder.addTextBody("qualifier", qualifier); builder.addTextBody("attribute", attributeName); InputStream dataStream = null; log.info("Payload: " + payload.toString()); if (payload != null) { log.info("Payload exists as an asset"); dataStream = payload.getOriginal().getStream(); } if(dataStream != null) { try { builder.addBinaryBody("file", dataStream); HttpEntity multipart = builder.build(); post.setEntity(multipart); try (CloseableHttpResponse response = _client.execute(post)) { int statusCode = response.getStatusLine().getStatusCode(); String responseBody = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8); if (statusCode == 200) { log.debug("Response: " + responseBody); ObjectMapper mapper = new ObjectMapper(); return mapper.readValue(responseBody, ItemKeyInfo.class); } else { log.error("Error uploading file to DFR, status code: " + statusCode); log.error("Response: " + responseBody); return null; } } } finally { dataStream.close(); } } else { String message = "No data found in payload: " + payload.getPath().toString(); log.error(message); throw new WorkflowException(message);

Is there any reason you see that the file would not be posted.  Here is the log from the api on the other side receiving the request:

DEBUG - (7) AddFileToItem data: ItemNumber: 60057, Revision: <No Revision>, Qualifier: Part.0, Attribute: BTF Imagery, File: <No File>

 

As you can see all parameters make it except the file.   Am I missing something obvious?

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by ChristopherHo5

The problem was the addBinaryBody method used was not the correct one.  If you use addBinaryBody and specify the content type and filename it works.

1 reply

ChristopherHo5AuthorAccepted solution
Level 2
August 27, 2024

The problem was the addBinaryBody method used was not the correct one.  If you use addBinaryBody and specify the content type and filename it works.