eclipse-thingweb/node-wot

Response not valid if charset is indicated in header

egekorkan opened this issue · 1 comments

In line

if (content.type !== form.response.contentType) {
, there is a check whether the header content type matches the content type in the TD. This is a strict string check but the header can actually contain charset information. For example, JSON is UTF-8 encoded by default so there is no need to indicate "contentType":"application/json; charset=utf-8 in the TD. However, some HTTP servers like the popular expressjs put the charset information to be sure. So an expressjs Thing consumed by node-wot MUST have "contentType":"application/json; charset=utf-8 otherwise one gets the following error:

node-wot:binding-http:http-client-impl:debug HttpClient received Content-Type: application/json; charset=utf-8 +1ms
Error: Unexpected type in response

So one side, the WoT specs should be more clear on this possibility but node-wot needs to be more flexible as well. Since this code is in the consumed thing of core, we can only check for inclusion of the form's content type in the response header but not a direct comparison. A more elegant solution would be to move these kinds of checks to each content serdes implementation. This way, JSON content serdes, which knows what are the defaults, can do the checks.

Found the bug via eclipse-thingweb/test-things#16

The interesting part is that this check is only there for an action whose form contain a response where the payload for input and output have different media types. It is fine if a property read results with a "application/json; charset=utf-8". In this case, the easiest solution would be to remove that check in the action.