DMTF/python-redfish-library

Proper format for multipart/form-data POST request

grant-he opened this issue · 8 comments

Based on Redfish specification, I need to call a multipart/form-data POST request to upload BMC and BIOS files. What is the proper syntax or format to perform such a call from a redfish_client object? I have successfully called the POST request that enters "update mode" and am just having issues making the subsequent call correctly.

I've tried many different iterations of the call but none have worked. Here is my current version:

with open(bmc_path, "rb") as file:
    headers = {"Content-Type": "multipart/form-data"}
    body = {"bmc": (bmc_filename, file)}
    response = redfish_client.post(<API URL>, body=body, headers=headers)

Unfortunately it doesn't appear we really have support in the library for making such a request. When performing a request to the library with a body, the library will automatically a new value for Content-Type based on the data for the body specified. In addition, the body in this type of request will take multiple data types; with what you have specified currently, it's still considered a Python dictionary, so the library will interpret the request a "application/json".

We certainly should add support for multipart/form-data considering this is part of the standard.

Hi! What is the implementation status of this feature? If there is no progress yet, I could work on a patch.

I have not had a chance to take a look at this yet. Would certainly welcome help in the space if you have free time to make a pull request.

Is it fine to require the urllib3 package or is it an explicit goal to not have additional external dependencies?

That should be okay. To be honest, I'm not 100% sure why that package wasn't used as a baseline in general for this library. I'll do some checking around to see if there was any reason to avoid it in the first place, but I don't see any issue with it.

The requests package might be another good option; I think I've heard folks mention is might be a good idea to leverage requests here in general. Again, I need to do more digging as to why the library originally went the path it did.

The requests package is indeed a great option for these types of libraries in general. However, we would additionally need requests_toolbelt to format the multipart request body as described by the Redfish specification [1].

Let me know if you find out anything about the reasoning of the original design choices in the library. Cheers!

I spoke to a few others and as far as we can tell there was no rationale beyond the original author likely did not know about some of these other modules.

That's a good reason to avoid using requests for this particular purpose. I'll review your PR by the end of the week; thanks for taking on this work!