Hi,
I am trying to update a domain name on a machine by using the PUT endpoint of the API. I am wrapping everything in Python with the “Requests” module.
The code looks like this.
def set_machine_domain_name(maas_api_url, machine_id, maas_key, domain_name):
auth = get_maas_oauth_token(maas_key)
print(maas_api_url + “machines/” + machine_id + “/”)
r = requests.put(maas_api_url + “machines/” + machine_id + “/”,
headers=GET_HEADERS,
auth=auth,
data={‘domain’: domain_name
}
)
I can use the very same url for a POST/GET without any issues so I know that the URL is valid. Is there anything different in how MAAS handles a PUT request?
Thanks!
Hello,
I was facing a similar issue and I could solve it by changing the MIME Type for the PUT requests to content-type: multipart/form-data;, instead of Content-Type: application/x-www-form-urlencoded that is produced when using the data parameter in requests.
To make that happen, the easiest way is to use the files parameter with a custom payload. Here is a code snippet that might help on this:
from requests_oauthlib import OAuth1Session
consumer_key, key, secret = apikey.split(':')
session = OAuth1Session(
consumer_key, resource_owner_key=key, resource_owner_secret=secret
)
if method == 'POST':
resp = session.request(method, url, data=params, verify=False)
elif method == 'PUT':
files_payload = {}
for param in params.keys():
files_payload[param] = (None, params[param], 'text/plain; charset="utf-8"')
resp = session.request(method, url, files=files_payload, verify=False)
elif method == 'GET':
resp = session.request(method, url, params=params, verify=False)
Hope it helps.