question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

CloudEvent with no datacontenttype parsed as JSON instead of plain text

See original GitHub issue

Describe the Bug Doing a demo based on this blog to consume S3 events from Kamelet The nodejs SDK can’t handle the HTTP POST request with cloud event Using the following code https://github.com/knative/docs/blob/main/docs/serving/samples/cloudevents/cloudevents-nodejs/index.js

Steps to Reproduce

  1. create nodejs service
  2. create trigger to send cloudevent from broker to nodejs service

Expected Behavior That the cloud event is properly handle. The knative event-display using the goland cloudevent can handle plaintext in body and missing content-type in header. https://github.com/knative/eventing/blob/main/cmd/event_display/main.go#L59

Additional context

Try both versions of the SDK 4.0.3 and 5.0.0 same error

The input event from Camel Kamelet S3 is the following

Debug Incoming Request: {
   "headers": {
     "host": "func1.default.svc.cluster.local",
     "user-agent": "Go-http-client/1.1",
     "content-length": "11",
     "accept-encoding": "gzip",
     "ce-id": "A8A6BD24B2AE5FF-0000000000000017",
     "ce-knativearrivaltime": "2021-11-17T22:48:50.315578709Z",
     "ce-source": "source",
     "ce-specversion": "1.0",
     "ce-time": "2021-11-17T22:48:50.306Z",
     "ce-type": "org.apache.camel.event",
     "forwarded": "for=10.131.0.156;proto=http",
     "k-proxy-request": "activator",
     "traceparent": "00-0f3421c775056be4f762f87b5cee145d-5f24409ef4edc327-00",
     "x-forwarded-for": "10.131.0.156, 10.128.2.48",
     "x-forwarded-proto": "http",
     "x-request-id": "13396d30-5487-46c1-9d21-b3fd675447e0"
   },
   "body": "test func1\n"
 }

The error is the following

SyntaxError: Unexpected token 
  in JSON at position 11
     at JSON.parse (<anonymous>)
     at parseJSON (/opt/app-root/src/node_modules/cloudevents/dist/parsers.js:39:67)
     at JSONParser.parse (/opt/app-root/src/node_modules/cloudevents/dist/parsers.js:40:16)
     at parseBinary (/opt/app-root/src/node_modules/cloudevents/dist/message/http/index.js:166:23)
     at Object.deserialize [as toEvent] (/opt/app-root/src/node_modules/cloudevents/dist/message/http/index.js:86:20)
     at /opt/app-root/src/index.js:70:24
     at Layer.handle [as handle_request] (/opt/app-root/src/node_modules/express/lib/router/layer.js:95:5)
     at next (/opt/app-root/src/node_modules/express/lib/router/route.js:137:13)
     at Route.dispatch (/opt/app-root/src/node_modules/express/lib/router/route.js:112:3)
     at Layer.handle [as handle_request] (/opt/app-root/src/node_modules/express/lib/router/layer.js:95:5)

cc @lance

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
duglincommented, Nov 19, 2021

Seems to me that the SDK should not be parsing the body at all - it’s binary and should be passed on to the app as an array of bytes. If there was a content-type header that says it is JSON, then the SDK might try to be smart and convert it to a JSON object, but w/o that it seems like the SDK has no choice but to treat it as an array of bytes, not JSON.

0reactions
lancecommented, Nov 19, 2021

Seems to me that the SDK should not be parsing the body at all - it’s binary and should be passed on to the app as an array of bytes.

I’ll be willing to bet this is what the Go SDK is doing.

Read more comments on GitHub >

github_iconTop Results From Across the Web

handling of datacontenttype is inconsistent #558 - GitHub
To sum it up, it is not possible to put a JSON-encoded data blob into a CloudEvent; and a parser needs to treat...
Read more >
JSDoc: Source: cloudevent.js - smartiniongithub.github.com
isCloudEvent(event)) {; throw new TypeError('The given event is not a CloudEvent instance'); }; return (; (event.datacontenttype === CloudEvent.
Read more >
Event - Go Packages
Package event provides primitives to work with CloudEvents specification: ... const ( TextPlain = "text/plain" TextJSON = "text/json" ...
Read more >
Persisting Cloud Events to Cosmos DB in Azure
NET 5 release is just around the corner, we're going to pick the . ... However, if you select application/cloudevents+json from the content ......
Read more >
CloudEvent.ParseMany(BinaryData, Boolean) Method
Given JSON-encoded events, parses the event envelope and returns an array of CloudEvents. If the specified event is not valid JSON an exception...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found