openfaas / connector-sdk

SDK for connecting events to functions
MIT License
54 stars 25 forks source link

Connector-sdk does not set the Content-type when invoking a function #58

Closed cpanato closed 3 years ago

cpanato commented 3 years ago

My actions before raising this issue

I did a pair session with Alex some weeks ago about the [cloudnative bot] and I added a check to validate the content type: https://github.com/cpanato/cloudnative-bot/blob/main/functions/k8s-patch-release/handler.go#L59

I disable that because looks like the connector-sdk looks like does not set the content type

I’ve made a simple project with the connector-sdk + a very simple function and i can see that is not set

I checked the code for connector-sdk and did not see anything that sets this header

Expected Behaviour

Set the content-type header header

Current Behaviour

not setting the content-type header

Possible Solution

Add and option when invoking the function to pass a set of headers that the user want to forward to the function

maybe add another parementer in the invoke function: https://github.com/openfaas/connector-sdk/blob/master/cmd/tester/main.go#L50

Steps to Reproduce (for bugs)

  1. can use the example https://github.com/openfaas/connector-sdk/blob/master/cmd/tester/main.go to run your streaming

  2. create a simple function the check the header and deploy it

    func Handle(req []byte) string {
    log.Printf("Content Type: %s", os.Getenv("Http_Content_Type"))
    if os.Getenv("Http_Content_Type") != "application/json" {
        return "Invalid Content Type"
    }
    
    log.Println(string(req))
    if req == nil {
        log.Println("nothing to process")
        return "nothing to process"
    }
    
    return fmt.Sprintf("Hello, Go. You said: %s", string(req))
    }
  3. run the service describe in item 1 and deploy item 2

Context

Your Environment

Next steps

You may join Slack for community support.

alexellis commented 3 years ago

My suggestion would be to add the content-type as a configuration item to: ControllerConfig

https://github.com/openfaas/connector-sdk/blob/master/types/controller.go#L17

This would be set statically at deploy-time for all messages sent via the invoke method.

The Controller would then pass that value on to the Invoker here:

https://github.com/openfaas/connector-sdk/blob/master/types/invoker.go#L40

I haven't seen a need from users yet, or our own use for variable content-types depending on the message topic, so let's keep it simple for iteration #1.

So to sum up: set a Content-Type configuration item via an environment variable and pass it down the stack to where it's needed. Keep everything else the same, and then update the tester example and README please.

Once the PR is up, let's get it reviewed and released so that it can be retro-fitted to the nats-connector and others like your own.

Alex