akrabat opened a new issue #2630: JSON encoding is inconsistent for web actions
URL: https://github.com/apache/incubator-openwhisk/issues/2630
 
 
   There's something very odd about JSON encoding and content types with web 
actions.
   
   ```php
   <?php
   function main(array $args) : array
   {
       return [
           "body" => ["greeting" => "Hello world"],
           "headers" => ["Content-Type" => "application/json"],
       ];
   }
   ```
   gives:
   ```
   $ curl -ki https://192.168.33.13/api/v1/web/guest/default/hello
   HTTP/1.1 200 OK
   Content-Type: application/json
   
   {"greeting":"Hello world"}
   ```
   
   All good.
   
   However, for:
   
   ```php
   <?php
   function main(array $args) : array
   {
       return [
           "body" => ["greeting" => "Hello world"],
           "headers" => ["Content-Type" => "application/json-patch+json"],
       ];
   }
   ```
   I get:
   ```
   $ curl -ki https://192.168.33.13/api/v1/web/guest/default/hello
   HTTP/1.1 400 Bad Request
   Content-Type: application/json
   
   {"error":"Response type in header did not match generated content 
type.","code":48}
   ```
   Complete failure. Maybe it's because the new 
"no-need-to-base64-a-json-string" code is hard coded to only support the 
`application/json` content type?
   
   However, if I base64 encode & stringify the JSON:
   
   ```php
   <?php
   function main(array $args) : array
   {
       return [
           "body" => base64_encode(json_encode(["greeting" => "Hello world"])),
           "headers" => ["Content-Type" => "application/json-patch+json"],
       ];
   }
   ```
   then this happens:
   ```
   $ curl -ki https://192.168.33.13/api/v1/web/guest/default/hello
   HTTP/1.1 200 OK
   Content-Type: application/json-patch+json
   
   eyJncmVldGluZyI6IkhlbGxvIHdvcmxkIn0=
   ```
   Content type is right, but the body isn't decoded!
   
   My final test was using `application/problem+json` and this does something 
different again:
   
   ```php
   <?php
   function main(array $args) : array
   {
       return [
           "body" => base64_encode(json_encode(["greeting" => "Hello world"])),
           "headers" => ["Content-Type" => "application/problem+json"],
       ];
   }
   ```
   results in:
   ```
   $ curl -ki https://192.168.33.13/api/v1/web/guest/default/hello
   HTTP/1.1 400 Bad Request
   Content-Type: application/json
   
   {"error":"Response did not specify a known content-type.","code":54}
   ```
   
   It hasn't worked at all! Obviously `application/problem+json` is a valid 
content type as it's defined in [RFC7807](https://tools.ietf.org/html/rfc7807), 
so my guess is that it's not the right list?  Clearly, we can't have a 
whitelist for content types.
   
   
   As I say, something's odd here.
 
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to