knative/func

Func invoke encodes json data differently than curl

Closed this issue · 2 comments

When sending events with json data with func invoke, the way in which the data is encoded in the event behaves differently than when using curl to call the function, which is confusing.

To reproduce:

  1. Clone https://github.com/Cali0707/func-invoke-confusion
  2. Run the function with func run
  3. Send an event to the function with curl:
curl -X POST http://localhost:8080/ \
 -H "ce-id: 12345" \
 -H "ce-source: curl" \
 -H "ce-type: this.will.work" \
 -H "ce-specversion: 1.0" \
 -H "Content-Type: application/json" \
 -d '{"customerId": "0123456", "productId": "6543210"}'
  1. Send an event to the function with func invoke:
func invoke --data '{"customerId": "0123456", "productId": "6543210"}'
  1. Check the logs from the function, they will look like:
listening on http port 8080
Running on host port 8080
Received event
Context Attributes,
  specversion: 1.0
  type: this.will.work
  source: curl
  id: 12345
  datacontenttype: application/json
Data,
  {
    "customerId": "0123456",
    "productId": "6543210"
  }

Successfully decoded purchase: &{CustomerId:0123456 ProductId:6543210}Received event
Context Attributes,
  specversion: 1.0
  type: boson.fn
  source: /boson/fn
  id: 0385ddeb-043a-472d-93a9-e7b281ba319a
  time: 2024-03-20T20:53:50.985375048Z
  datacontenttype: application/json
Data,
  "{\"customerId\": \"0123456\", \"productId\": \"6543210\"}"

Failed to decode event:  [json] found bytes ""{\"customerId\": \"0123456\", \"productId\": \"6543210\"}"", but failed to unmarshal: json: cannot unmarshal string into Go value of type function.Purchase

I'm not sure if this is the expected behaviour or not, but if it is it might be worth mentioning this somewhere, as I expected that it would behave the same way as curl and send json, rather than a json encoded string.

I think this is definitely worth mentioning, and I would expect the encoding to work the same way in the examples you showed as well.

/assign