@@ -58,15 +58,15 @@ func init() {
5858}
5959
6060var invokeCmd = & cobra.Command {
61- Use : `invoke FUNCTION_NAME [--gateway GATEWAY_URL] [--content-type CONTENT_TYPE] [--query PARAM =VALUE] [--header PARAM= VALUE] [--method HTTP_METHOD]` ,
61+ Use : `invoke FUNCTION_NAME [--gateway GATEWAY_URL] [--content-type CONTENT_TYPE] [--query KEY =VALUE] [--header "KEY: VALUE" ] [--method HTTP_METHOD]` ,
6262 Short : "Invoke an OpenFaaS function" ,
6363 Long : `Invokes an OpenFaaS function and reads from STDIN for the body of the request` ,
64- Example : ` faas-cli invoke echo --gateway https://host:port
64+ Example : ` faas-cli invoke printer --gateway https://host:port <<< "Hello"
6565 faas-cli invoke echo --gateway https://host:port --content-type application/json
6666 faas-cli invoke env --query repo=faas-cli --query org=openfaas
67- faas-cli invoke env --header X-Ping-Url= http://request.bin/etc
68- faas-cli invoke resize-img --async -H "X-Callback-Url= http://gateway:8080/function/send2slack" < image.png
69- faas-cli invoke env -H X-Ping-Url= http://request.bin/etc
67+ faas-cli invoke env --header " X-Ping-Url: http://request.bin/etc"
68+ faas-cli invoke resize-img --async -H "X-Callback-Url: http://gateway:8080/function/send2slack" < image.png
69+ faas-cli invoke env -H X-Ping-Url: http://request.bin/etc
7070 faas-cli invoke flask --method GET --namespace dev
7171 faas-cli invoke env --sign X-GitHub-Event --key yoursecret` ,
7272 RunE : runInvoke ,
@@ -118,7 +118,10 @@ func runInvoke(cmd *cobra.Command, args []string) error {
118118 return err
119119 }
120120
121- httpHeader .Set ("Content-Type" , contentType )
121+ if httpHeader .Get ("Content-Type" ) == "" || cmd .Flag ("content-type" ).Changed {
122+ httpHeader .Set ("Content-Type" , contentType )
123+ }
124+
122125 httpHeader .Set ("User-Agent" , fmt .Sprintf ("faas-cli/%s (openfaas; %s; %s)" , version .BuildVersion (), runtime .GOOS , runtime .GOARCH ))
123126
124127 stat , _ := os .Stdin .Stat ()
@@ -219,23 +222,49 @@ func missingSignFlag(header string, key string) bool {
219222// parseHeaders parses header values from the header command flag
220223func parseHeaders (headers []string ) (http.Header , error ) {
221224 httpHeader := http.Header {}
225+ warningShown := false
222226
223227 for _ , header := range headers {
224- headerVal := strings .SplitN (header , "=" , 2 )
225- if len (headerVal ) != 2 {
226- return httpHeader , fmt .Errorf ("the --header or -H flag must take the form of key=value" )
227- }
228+ // First try the preferred Key: Value format
229+ parts := strings .SplitN (header , ":" , 2 )
230+ if len (parts ) == 2 {
231+ key := strings .TrimSpace (parts [0 ])
232+ if key == "" {
233+ return httpHeader , fmt .Errorf ("the --header or -H flag must take the form of 'Key: Value' (empty key given)" )
234+ }
228235
229- key , value := headerVal [0 ], headerVal [1 ]
230- if key == "" {
231- return httpHeader , fmt .Errorf ("the --header or -H flag must take the form of key=value (empty key given)" )
236+ value := strings .TrimSpace (parts [1 ])
237+ if value == "" {
238+ return httpHeader , fmt .Errorf ("the --header or -H flag must take the form of 'Key: Value' (empty value given)" )
239+ }
240+
241+ httpHeader .Add (key , value )
242+ continue
232243 }
233244
234- if value == "" {
235- return httpHeader , fmt .Errorf ("the --header or -H flag must take the form of key=value (empty value given)" )
245+ // Fallback to deprecated key=value format
246+ parts = strings .SplitN (header , "=" , 2 )
247+ if len (parts ) == 2 {
248+ key := parts [0 ]
249+ if key == "" {
250+ return httpHeader , fmt .Errorf ("the --header or -H flag must take the form of 'Key: Value' or 'key=value' (empty key given)" )
251+ }
252+
253+ value := parts [1 ]
254+ if value == "" {
255+ return httpHeader , fmt .Errorf ("the --header or -H flag must take the form of 'Key: Value' or 'key=value' (empty value given)" )
256+ }
257+
258+ // Print deprecation warning only once
259+ if ! warningShown {
260+ fmt .Fprintf (os .Stderr , "Warning: Using deprecated 'key=value' format for headers. Please use 'Key: Value' format instead.\n " )
261+ warningShown = true
262+ }
263+ httpHeader .Add (key , value )
264+ continue
236265 }
237266
238- httpHeader . Add ( key , value )
267+ return httpHeader , fmt . Errorf ( "the --header or -H flag must take the form of 'Key: Value' or 'key= value'" )
239268 }
240269
241270 return httpHeader , nil
0 commit comments