Skip to content

Support for lack of content-type header in binary mode parsing #117

Closed
@aliok

Description

@aliok

I have a very simple binary receiver:

const v1 = require("cloudevents-sdk/v1");
const receiver = new v1.BinaryHTTPReceiver();

const express = require('express')
const app = express()
const port = 8080

app.use((req, res, next) => {
    let data = "";

    //req.setEncoding("utf8");
    req.on("data", function(chunk) {
        data += chunk;
    });

    req.on("end", function() {
        req.body = data;
        next();
    });
});

app.post("/", function(req, res) {
    console.log("HEADERS===========");
    console.log(req.headers);
    console.log("BODY===========");
    console.log(req.body);

    try {
        const myevent = receiver.parse(req.body, req.headers);
        // pretty print
        console.log(new Date() +  " Accepted event. Type: " + myevent.getType() + ", data: " + JSON.stringify(myevent.getData(), null));

        res.status(201).json(myevent.format());
    } catch (err) {
        console.log("ERROR===========");
        console.error(err);
        console.log("===========");
        res.status(415)
            .header("Content-Type", "application/json")
            .send(JSON.stringify(err));
    }
});

app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`))

When I send a request without Content-Type header, I see an error in the logs:

curl -X POST \
     -d'{"hello":"world"}' \
     -H'Content-Type:' \
     -H'ce-specversion:1.0' \
     -H'ce-type:com.github.pull.create' \
     -H'ce-source:https://github.com/cloudevents/spec/pull/123' \
     -H'ce-id:45c83279-c8a1-4db6-a703-b3768db93887' \
     -H'ce-time:2019-11-06T11:17:00Z' \
     http://localhost:8080

Logs:

HEADERS===========
{
  host: 'localhost:8080',
  'user-agent': 'curl/7.66.0',
  accept: '*/*',
  'ce-specversion': '1.0',
  'ce-type': 'com.github.pull.create',
  'ce-source': 'https://github.com/cloudevents/spec/pull/123',
  'ce-id': '45c83279-c8a1-4db6-a703-b3768db93887',
  'ce-time': '2019-11-06T11:17:00Z',
  'content-length': '17'
}
BODY===========
{"hello":"world"}
ERROR===========
{ message: 'invalid content type', errors: [ undefined ] }
===========

I understand that this is a supported case in Golang SDK as lack of content type means application/json (maybe I am wrong).
Although I couldn't find anything like that in the CloudEvents spec.

My use case is, that I have a Knative Kafka event source that's sending binary cloudevents without any Content-Type set.

Metadata

Metadata

Assignees

Labels

type/bugSomething isn't workingversion/1.xIssues related to version 1.x of this libraryversion/1.x.backportChanges that should be backported to the 1.x release line

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions