Can't get "requests.put" to work in OctoPrint

I swear this was working in OctoPrint 1.3.11 and earlier. A local request to the REST API seems to now be freezing up within a plugin.


I didn't have the data= in the second argument to requests.put. I was also doing all this in Py2.


Now I'm in Py3 and 1.4.rc3. Having researched a little, I'm guessing that the newer requests module wants that data= prepended to the second argument.

def __set_user_password(apiKey, username, password):
    if apiKey == op.octoprintApiKey:
        op.logger.debug('__set_user_password calling REST API [' + username + '][' + password + ']')
        api_url =                           op.octoprintBaseUrl + '/api/users/' + username + '/password'
        op.logger.debug('__set_user_password [' + api_url + ']')
        op.logger.debug('__set_user_password [' + apiKey + ']')
        jsonBody =                          json.dumps({"password":str(password)})
        op.logger.debug('__set_user_password [' + str(jsonBody) + ']')
        r =                                 requests.put(api_url, data=jsonBody, headers={"X-Api-Key":apiKey,"Content-Type":"application/json"})
        self.logger.debug('__set_user_password api returned ' + str(r.status_code))
        if r.status_code == 200:
            del r
            return True
        del r
    return False

Simply trying to set my password to password yields (in my log):

[DEBUG]  __set_user_password calling REST API [me][password]
[DEBUG]  __set_user_password [http://localhost/api/users/me/password]
[DEBUG]  __set_user_password [CORRECT-API-KEY]
[DEBUG]  __set_user_password [{"password": "password"}]
---- Freeze-up ------------------------------------------------------------------------------------------------

I do the same with Postman and of course it returns immediately from that software. I note that the REST API is called, OctoPrint does make the update to the indicated password. The timestamp on the ~/.octoprint/users.yaml matches the attempt's time. The MD5 hash is correctly stored for the password change in this file.

Instead of the REST API call locally, I also tried swapping it for this:

            op.logger.debug('__set_user_password [' + username + '][' + password + ']')
            op.obj.user_manager.change_user_password(username, password)
            self.logger.debug('__set_user_password changed password')
            return True
        except UnknownUser:
            self.logger.debug('__set_user_password trapped exception: invalid user')

Same result; at the call it just freezes the plugin code.

Nevermind, I was being stupid: I mismatched my logging. Note the deadly self.logger... in both sets of attempts. You'd think that this might bubble up to any log as an exception or a problem but nope.

1 Like

Can't tell you how many times that has bit me.

My problem was that I was predisposed to believe that this was yet another Py3 difference.