Skip to content

Commit

Permalink
Merge pull request #100 from DMTF/File-Option-For-Raw
Browse files Browse the repository at this point in the history
Extended 'rf_raw_request.py' to allow it to send binary data from a file
  • Loading branch information
mraineri authored Apr 25, 2023
2 parents e38503d + 4b04b72 commit 602cf7a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down
36 changes: 24 additions & 12 deletions scripts/rf_raw_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import argparse
import json
import os
import redfish

# Get the input arguments
Expand All @@ -22,18 +23,26 @@
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
if body is None:
# Default case if nothing resolves (empty JSON object)
body = {}

# Perform the requested operation
if args.method == "HEAD":
Expand All @@ -57,8 +66,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" )

0 comments on commit 602cf7a

Please sign in to comment.