PATCH request exception
antesaj opened this issue · 13 comments
I'm getting an unhandled exception when attempting to send a PATCH request to the Redfish Mockup Server via Node JS. See the console output below.
content-type: application/x-www-form-urlencoded
accept: application/json
content-length: 25
Connection: close
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 51888)
Traceback (most recent call last):
File "Z:\Python\lib\socketserver.py", line 317, in _handle_request_noblock
self.process_request(request, client_address)
File "Z:\Python\lib\socketserver.py", line 348, in process_request
self.finish_request(request, client_address)
File "Z:\Python\lib\socketserver.py", line 361, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "Z:\Python\lib\socketserver.py", line 696, in __init__
self.handle()
File "Z:\Python\lib\http\server.py", line 418, in handle
self.handle_one_request()
File "Z:\Python\lib\http\server.py", line 406, in handle_one_request
method()
File "redfishMockupServer.py", line 226, in do_PATCH
dataa = json.loads(self.rfile.read(lenth).decode("utf-8")) # Error is here
File "Z:\Python\lib\json\__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "Z:\Python\lib\json\decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "Z:\Python\lib\json\decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
----------------------------------------```
Is there a body in the request? PATCH is supposed to be accompanied with an HTTP body containing the JSON payload with the properties to modify. If this is the case, we should still handle it gracefully, but just need to understand the context before going forward with changes.
I changed the Node PATCH request to contain explicitly 'body' data instead of form data. This stopped the exceptions from occurring, however, are the PATCH requests meant to modify the actual contents on disk? I'm attempting to PATCH a metric report definition's Status.State to "Enabled"
Status.State is a read-only property as defined in the schema, so a PATCH will fail. But the tool should certainly handle that error gracefully. The specification states this case should return HTTP 400 in this case as long as the PATCH was for a single property.
Ah, I've been thinking that in order for Redfish API metrics to be accessible, the parent metric report had to be enabled, and that PATCHing Status.State in the definition was the proper way to make them become accessible. I'll have to dig into the docs some more.
In any case, it looks like the tool is sending a 204 in response to PATCHing that particular attribute of the resource.
Yes, here is the Node.js PATCH request code, including the payload (the body object):
request.patch(
{
url: `http://localhost:8001/redfish/v1/TelemetryService/MetricReportDefinitions/CPUConfig`,
json: true,
body: {
Status: {
State: "Enabled"
}
}
},
(error, response, body) => {
console.log(response);
}
);
I managed to run this on my machine through the nodejs repl, and at least did not get an exception at that pt, using the master
branch. For the same command, however, I get:
Serving Mockup in abs real directory path:/home/tomp/redfish/Redfish-Mockup-Server/public-bladed
Serving Redfish mockup on port: 8000
running Server...
PATCH: Headers: host: localhost:8000
accept: application/json
content-type: application/json
content-length: 30
Connection: close
PATCH: Data: {'Status': {'State': 'Enabled'}}
application/json
{'Status': {'State': 'Enabled'}}
{}
{'Status': {'State': 'Enabled'}}
127.0.0.1 - - [29/Jan/2019 20:25:11] "PATCH /redfish/v1/TelemetryService/MetricReportDefinitions/CPUConfig HTTP/1.1" 204 -
which might be some sort of system difference? Notably on content-length and content-type?
@tomasg2012 The exception was occurring during an attempt to send form data; when I switched the object to body, I too no longer receive an exception. In fact, I am receiving a 204 for a PATCH that shouldn't be possible, which is the new issue at this point.
If you change the word body to form in the code, you should reproduce the exception.
On another note, shouldn't there be a way to enable a metric report? Is there a way besides PATCHing that option?
I think that's been the note of one of the issues #45, or simply placing a metric report into your server. However, the mockup server is not 100% to spec on PATCH as its (at least, in my philosophy) mostly for scratch work.
We need to determine how to enable a MetricReport based on standard properties… and we have a MetricReportDefinition with a Status.State... but no obvious way to "enable/disable" it.
Scratch work or not, the Redfish-Mockup-Server is being used by several teams in multiple countries and should handle error per spec. If we have identified an issue with how PATCH is handled, please resolve appropriately.