From 89743df70b2ed9d240a2dd21d7e5246cb47ddbec Mon Sep 17 00:00:00 2001 From: Mike Raineri Date: Mon, 24 Apr 2023 09:56:20 -0400 Subject: [PATCH 1/2] Extended 'rf_raw_request.py' to allow it to send binary data from a file Signed-off-by: Mike Raineri --- README.md | 4 +++- scripts/rf_raw_request.py | 33 +++++++++++++++++++++------------ 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 0f8d864..b93213a 100644 --- a/README.md +++ b/README.md @@ -786,7 +786,9 @@ optional arguments: --method {GET,HEAD,POST,PATCH,PUT,DELETE}, -m {GET,HEAD,POST,PATCH,PUT,DELETE} The HTTP method to perform; performs GET if not specified - --body BODY, -b BODY The body to provide with the request + --body BODY, -b BODY The body to provide with the request; can be a JSON + string for a JSON request, a filename to send binary + data, or an unstructured string --verbose, -v Indicates if HTTP response codes and headers are displayed ``` diff --git a/scripts/rf_raw_request.py b/scripts/rf_raw_request.py index b78adfb..65f610c 100644 --- a/scripts/rf_raw_request.py +++ b/scripts/rf_raw_request.py @@ -13,6 +13,7 @@ import argparse import json +import os import redfish # Get the input arguments @@ -22,18 +23,23 @@ argget.add_argument( "--rhost", "-r", type = str, required = True, help = "The address of the Redfish service (with scheme)" ) argget.add_argument( "--method", "-m", type = str, required = False, help = "The HTTP method to perform; performs GET if not specified", default = "GET", choices = [ "GET", "HEAD", "POST", "PATCH", "PUT", "DELETE" ] ) argget.add_argument( "--request", "-req", type = str, required = True, help = "The URI for the request" ) -argget.add_argument( "--body", "-b", type = str, required = False, help = "The body to provide with the request" ) +argget.add_argument( "--body", "-b", type = str, required = False, help = "The body to provide with the request; can be a JSON string for a JSON request, a filename to send binary data, or an unstructured string" ) argget.add_argument( "--verbose", "-v", action = "store_true", help = "Indicates if HTTP response codes and headers are displayed", default = False ) args = argget.parse_args() # Connect to the service with redfish.redfish_client( base_url = args.rhost, username = args.user, password = args.password ) as redfish_obj: - # Encode the body as a dictionary - try: - body = json.loads( args.body ) - except: - # Not valid JSON; try passing it into the request as a string - body = args.body + # Encode the body + # If the body argument points to a file, load the file + if args.body is not None and os.path.isfile(args.body): + with open(args.body, mode="rb") as file: + body = file.read() + else: + # Not a file; either JSON or a raw string + try: + body = json.loads( args.body ) + except: + body = args.body # Perform the requested operation if args.method == "HEAD": @@ -57,8 +63,11 @@ print() # Print the response - try: - print( json.dumps( resp.dict, sort_keys = True, indent = 4, separators = ( ",", ": " ) ) ) - except: - # The response is either malformed JSON or not JSON at all - print( resp.text ) + if resp.status != 204: + try: + print( json.dumps( resp.dict, sort_keys = True, indent = 4, separators = ( ",", ": " ) ) ) + except: + # The response is either malformed JSON or not JSON at all + print( resp.text ) + else: + print( "No response body" ) From 4b04b72dc39f46488c92d8a51d1d48728362ac12 Mon Sep 17 00:00:00 2001 From: Mike Raineri Date: Mon, 24 Apr 2023 12:50:09 -0400 Subject: [PATCH 2/2] Send an empty JSON object if no body is specified Signed-off-by: Mike Raineri --- scripts/rf_raw_request.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/scripts/rf_raw_request.py b/scripts/rf_raw_request.py index 65f610c..f77dbe9 100644 --- a/scripts/rf_raw_request.py +++ b/scripts/rf_raw_request.py @@ -31,8 +31,8 @@ with redfish.redfish_client( base_url = args.rhost, username = args.user, password = args.password ) as redfish_obj: # Encode the body # If the body argument points to a file, load the file - if args.body is not None and os.path.isfile(args.body): - with open(args.body, mode="rb") as file: + if args.body is not None and os.path.isfile( args.body ): + with open( args.body, mode="rb" ) as file: body = file.read() else: # Not a file; either JSON or a raw string @@ -40,6 +40,9 @@ body = json.loads( args.body ) except: body = args.body + if body is None: + # Default case if nothing resolves (empty JSON object) + body = {} # Perform the requested operation if args.method == "HEAD":