Expand my Community achievements bar.

SOLVED

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

Avatar

Level 2

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?

1 Accepted Solution

Avatar

Correct answer by
Level 2

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.

View solution in original post

1 Reply

Avatar

Correct answer by
Level 2

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.