@@ -348,21 +348,49 @@ def _build_request_body(
348348
349349 :return: A JSON string representing the request body, or None if not applicable.
350350 """
351- if operation .method == "get" :
352- # Get operations don't have a body
351+ if operation .method in ("get" , "delete" ):
352+ # GET and DELETE operations don't have a body
353+ if ctx .raw_body is not None :
354+ print (
355+ f"--raw-body cannot be specified for actions with method { operation .method } " ,
356+ file = sys .stderr ,
357+ )
358+ sys .exit (ExitCodes .ARGUMENT_ERROR )
359+
353360 return None
354361
362+ param_names = {param .name for param in operation .params }
363+
364+ # Returns whether the given argument should be included in the request body
365+ def __should_include (key : str , value : Any ) -> bool :
366+ return value is not None and key not in param_names
367+
368+ # If the user has specified the --raw-body argument,
369+ # return it.
370+ if ctx .raw_body is not None :
371+ specified_keys = [
372+ k for k , v in vars (parsed_args ).items () if __should_include (k , v )
373+ ]
374+
375+ if len (specified_keys ) > 0 :
376+ print (
377+ "--raw-body cannot be specified with action arguments: "
378+ + ", " .join (sorted (f"--{ key } " for key in specified_keys )),
379+ file = sys .stderr ,
380+ )
381+ sys .exit (ExitCodes .ARGUMENT_ERROR )
382+
383+ return ctx .raw_body
384+
355385 # Merge defaults into body if applicable
356386 if ctx .defaults :
357387 parsed_args = ctx .config .update (parsed_args , operation .allowed_defaults )
358388
359- param_names = {param .name for param in operation .params }
360-
361389 expanded_json = {}
362390
363391 # Expand dotted keys into nested dictionaries
364392 for k , v in vars (parsed_args ).items ():
365- if v is None or k in param_names :
393+ if not __should_include ( k , v ) :
366394 continue
367395
368396 path_segments = get_path_segments (k )
0 commit comments