@@ -3,7 +3,6 @@ package functions
33import (
44 "bytes"
55 "context"
6- "encoding/json"
76 "errors"
87 "fmt"
98 "io"
@@ -30,8 +29,8 @@ type InvokeMessage struct {
3029 Source string
3130 Type string
3231 ContentType string
33- Data string
34- Format string //optional override for function-defined message format
32+ Data [] byte
33+ Format string // optional override for function-defined message format
3534}
3635
3736// NewInvokeMessage creates a new InvokeMessage with fields populated
@@ -41,7 +40,7 @@ func NewInvokeMessage() InvokeMessage {
4140 Source : DefaultInvokeSource ,
4241 Type : DefaultInvokeType ,
4342 ContentType : DefaultInvokeContentType ,
44- Data : DefaultInvokeData ,
43+ Data : [] byte ( DefaultInvokeData ) ,
4544 // Format override not set by default: value from function being preferred.
4645 }
4746}
@@ -50,7 +49,6 @@ func NewInvokeMessage() InvokeMessage {
5049// invocation message. Returned is metadata (such as HTTP headers or
5150// CloudEvent fields) and a stringified version of the payload.
5251func invoke (ctx context.Context , c * Client , f Function , target string , m InvokeMessage , verbose bool ) (metadata map [string ][]string , body string , err error ) {
53-
5452 // Get the first available route from 'local', 'remote', a named environment
5553 // or treat target
5654 route , err := invocationRoute (ctx , c , f , target ) // choose instance to invoke
@@ -153,20 +151,10 @@ func sendEvent(ctx context.Context, route string, m InvokeMessage, t http.RoundT
153151 event .SetID (m .ID )
154152 event .SetSource (m .Source )
155153 event .SetType (m .Type )
156- if m .ContentType == "application/json" {
157- var d interface {}
158- err = json .Unmarshal ([]byte (m .Data ), & d )
159- if err != nil {
160- return
161- }
162- err = event .SetData (m .ContentType , d )
163- if err != nil {
164- return
165- }
166- } else if err = event .SetData (m .ContentType , m .Data ); err != nil {
167- return
154+ err = event .SetData (m .ContentType , (m .Data ))
155+ if err != nil {
156+ return "" , fmt .Errorf ("cannot set data: %w" , err )
168157 }
169-
170158 c , err := cloudevents .NewClientHTTP (
171159 cloudevents .WithTarget (route ),
172160 cloudevents .WithRoundTripper (t ))
@@ -200,7 +188,7 @@ func sendPost(ctx context.Context, route string, m InvokeMessage, t http.RoundTr
200188 "Source" : {m .Source },
201189 "Type" : {m .Type },
202190 "ContentType" : {m .ContentType },
203- "Data" : {m .Data },
191+ "Data" : {string ( m .Data ) },
204192 }
205193 if verbose {
206194 fmt .Println ("Sending values" )
@@ -209,7 +197,7 @@ func sendPost(ctx context.Context, route string, m InvokeMessage, t http.RoundTr
209197 }
210198 }
211199
212- req , err := http .NewRequestWithContext (ctx , "POST" , route , bytes .NewBufferString (m .Data ))
200+ req , err := http .NewRequestWithContext (ctx , "POST" , route , bytes .NewReader (m .Data ))
213201 if err != nil {
214202 return nil , "" , fmt .Errorf ("failure to create request: %w" , err )
215203 }
0 commit comments