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.

Could not parse. Unknown encoding. Invalid content type or spec version

See original GitHub issue

Hi,

I am trying to parse events published by Kafka. These events are audit events, that are published by Kafka Brokers in case of for example, an authentication attempt.

Here are the details of the event:

Header contains :

content-type : application/cloudevents+json

Value Contains:

{
  "data" : { 
    "serviceName" : "crn:///kafka=JAlahGL_TUmLkqN_24KC2A",
    "methodName" : "kafka.Authentication",
    "resourceName" : "crn:///kafka=JAlahGL_TUmLkqN_24KC2A",
    "authenticationInfo" : {
      "principal" : "User:kafka",
      "metadata" : {
        "mechanism" : "SASL_PLAINTEXT/SCRAM-SHA-256",
        "identifier" : "kafka"
      }
    },
    "requestMetadata" : {
      "client_address" : "/10.44.168.161"
    },
    "result" : {
      "status" : "SUCCESS",
      "message" : ""
    }
  },
  "id" : "84040a3c-e5d4-46ea-b92d-462b020c86f8",
  "source" : "crn:///kafka=JAlahGL_TUmLkqN_24KC2A",
  "specversion" : "1.0",
  "type" : "io.confluent.kafka.server/authentication",
  "datacontenttype" : "application/json",
  "subject" : "crn:///kafka=JAlahGL_TUmLkqN_24KC2A",
  "time" : "2021-06-21T13:48:42.377Z",
  "confluentRouting" : {
    "route" : "confluent-audit-log-events"
  }
}

When I try to deserialize this message in a Kafka Consumer, using CloudEventDeserializer.class

props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, CloudEventDeserializer.class);

It throws an exception :

org.apache.kafka.common.errors.SerializationException: Error deserializing key/value for partition audit-producer-topic-1-0 at offset 6. If needed, please seek past the record to continue consumption.
Caused by: io.cloudevents.rw.CloudEventRWException: Could not parse. Unknown encoding. Invalid content type or spec version
	at io.cloudevents.rw.CloudEventRWException.newUnknownEncodingException(CloudEventRWException.java:201)
	at io.cloudevents.core.message.impl.MessageUtils.parseStructuredOrBinaryMessage(MessageUtils.java:80)

Upon inspection of the MesasgeUtils.java class, it has the following check

public static MessageReader parseStructuredOrBinaryMessage(Supplier<String> contentTypeHeaderReader, Function<EventFormat, MessageReader> structuredMessageFactory, Supplier<String> specVersionHeaderReader, Function<SpecVersion, MessageReader> binaryMessageFactory) throws CloudEventRWException {
        String ct = (String)contentTypeHeaderReader.get();
        if (ct != null) {
            EventFormat format = EventFormatProvider.getInstance().resolveFormat(ct);
            if (format != null) {
                return (MessageReader)structuredMessageFactory.apply(format);
            }

            if (ct.startsWith("application/cloudevents")) {
                throw CloudEventRWException.newUnknownEncodingException();
            }
        }

According to documentation, application/cloudevents+json is a valid content type, then why is an exception being thrown for that?

Any explanation around this would be useful…

Thanks in advance !!

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
sumitbauraicommented, Nov 9, 2021

Hi, Indeed including the jackson dependency resolves the deserialization issue.

1reaction
slinkydevelopercommented, Jun 24, 2021

Can you try to debug the EventFormatProvider and see if the JsonFormat is loaded inside it? From the stacktrace, the error seems to be that you don’t have the JsonFormat loaded properly…

Read more comments on GitHub >

github_iconTop Results From Across the Web

Could not parse. Unknown encoding. Invalid content type or ...
Hi, I am trying to parse events published by Kafka. ... Could not parse. Unknown encoding. Invalid content type or spec version #835....
Read more >
change the header Content-Type to application/cloudevents+ ...
Simplified the input but still got the same InvalidContentType error. The raw output of the publish event does contain "Content-Type": " ...
Read more >
Sending and Receiving Cloud Events with Kafka - Quarkus
First, the specversion indicates which version of Cloud Event it is using (1.0). The id field provides an id for that specific event....
Read more >
CloudEventRWException (CloudEvents - API 2.1.1 API)
This class is the exception Protocol Binding and Event Format implementers can use to signal ... Parameters: specVersion - the invalid input spec...
Read more >
kafka broker dispatcher: Failed to handle subscriber response
CloudEventRWException : Could not parse. Unknown encoding. Invalid content type or spec version\n\tat io.cloudevents.rw.CloudEventRWException ...
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