cancel
Showing results for 
Search instead for 
Did you mean: 

Wrong content type in REST-API

b_schnarr
Champ in-the-making
Champ in-the-making
Hello at all,

when you invoke the Activiti REST-API
runtime/process-instances/{process-instance-id}/diagram
, the content-type set by the REST-API is
text/html
. That leads to the problem that the diagram (png) can not be displayed in the browser because the browser only displays the raw-content of the png-diagram.
Shouln´t it be
image/png
?

I need to display the diagram with the current state in a browser. Any idea without changing the rest api?

Best regards
Ben
4 REPLIES 4

balsarori
Champ on-the-rise
Champ on-the-rise
Spring MVC uses the request's Accept header to set the response content type. When a request to the diagram is made from an IMG HTML tag the header will look something like

Accept: image/png,image/*;q=0.8,*/*;q=0.5

Which will produce a response of content type image/png. But when trying to access the image directly the request's Accept header will look something like

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

Which produces a response of content type text/html. Although, the code in getProcessInstanceDiagram sets the content type of the response to image/png

[java]
response.setContentType("image/png");
[/java]

That won't work since Spring MVC will change it afterwards depending on the request's Accept header. To force the response header to always be image/png the produces attribute can be used in the @RequestMapping annotation as follows

[java]
@RequestMapping(value="/runtime/process-instances/{processInstanceId}/diagram", method = RequestMethod.GET, produces="image/png")
[/java]

b_schnarr
Champ in-the-making
Champ in-the-making
Wow, thanks for this detailed solution!

balsarori
Champ on-the-rise
Champ on-the-rise
Actually, I'm glad that you brought up this issue. I faced this issue a couple of months ago and was planning to submit a pull for a fix but forgot to do that. So, thanks for reminding me Smiley Happy

This issue doesn't appear in tests since requests do not include Accept headers, therefore, Spring MVC will not alter the response content type.

Additionally, using the produces attribute of @RequestMapping annotation doesn't handle cases when trying to access a non-existing process instance diagram or when an exception is thrown. The response in those cases will be an ugly 'HTTP Status 500' page error instead of a json formatted response. A better solution could be using Spring MVC's ResponseEntity class.

I am working on a fix and will submit it a pull request soon.

balsarori
Champ on-the-rise
Champ on-the-rise