extJS& Chef Server API - 405方法不允许

时间:2016-10-19 20:32:38

标签: http extjs chef

我正在尝试向Chef服务器的用户/ USER_NAME端点发出POST请求,并继续收到错误响应消息 405 - Method Not Allowed

Fatal error: Uncaught exception 'Exception' with message '<html><head><title>405 Method Not Allowed</title></head><body><h1>Method Not Allowed</h1>Method Not Allowed<p><hr><address>mochiweb+webmachine web server</address></body></html>' in ..\..\..\Chef.php:191

但是,当我尝试使用GET方法发出相同的请求时,一切正常。

{username: "jsmith", email: "", display_name: "unknown", first_name: "John", last_name: "Smith",…}
city:"unknown"
country:"unknown"
display_name:"unknown"
email:""
first_name:"John"
last_name:"Smith"
middle_name:""
public_key:"-----BEGIN PUBLIC KEY----- -----END PUBLIC KEY-----"
username:"jsmith"

请注意,根据文档(Query For Users and Orgs),我使用了关键用户名和pivotal.pem文件。

以下是我发送信息的示例:

Example Chef Server User Request

正如我上面提到的,我唯一改变的是使用的方法(GET / POST)。

这是我正在使用的表单的extJS代码:

var enrollUserForm = Ext.create('Ext.form.Panel', {
    title: 'Enroll User',
    height: 200,
    width: 300,
    bodyPadding: 10,
    url: '../../../../chefEnrollUser.php',
    defaultType: 'textfield',
    items: [{
        fieldLabel: 'First Name',
        name: 'firstName'
    }, {
        fieldLabel: 'Last Name',
        name: 'lastName'
    }, {
        fieldLabel: 'Email Address',
        name: 'emailAddress'
    }, {
        fieldLabel: 'Username',
        name: 'username'
    }, {
        xtype: 'textfield',
        inputType: 'password',
        fieldLabel: 'Password',
        name: 'password'
    }],
    buttons: [{
        text: 'Enroll',
        formBind: true,
        handler: function() {
            var enrollUserInformation = enrollUserForm.getForm();
            var firstName = enrollUserInformation.findField('firstName')['value'];
            var lastName = enrollUserInformation.findField('lastName')['value'];
            var emailAddress = enrollUserInformation.findField('emailAddress')['value'];
            var username = enrollUserInformation.findField('username')['value'];
            var password = enrollUserInformation.findField('password')['value'];

            if (firstName != null && lastName != null && emailAddress != null && password != null) {
                if (firstName.length == 0 || lastName.length == 0 || emailAddress.length == 0 ||
                    username.length == 0 || password.length == 0) {
                    alert("Please fill in all fields");
                } else {
                    if (validateEmail(emailAddress)) {
                        // proceed to enrolling
                          enrollUserForm.getForm().submit({
                            params: {
                                firstName: firstName,
                                lastName: lastName,
                                emailAddress: emailAddress,
                                username: username,
                                password: password
                            }
                          })
                    } else {
                        alert("Please enter a valid email address.");
                    }
                }
            } else {
                alert("Please fill in all fields.");
            }
        }
    }]
});

这是我的chefEnrollUser.php文件:

<?php

namespace Jenssegers\Chef;

session_start();

header('Accept: application/json');
header('Content-type: application/json');

ini_set('display_errors', 'On');
error_reporting(E_ALL | E_STRICT);

// Retrieve parameters from Enroll User Form
$firstName    = $_REQUEST['firstName'];
$lastName     = $_REQUEST['lastName'];
$emailAddress = $_REQUEST['emailAddress'];
$username     = $_REQUEST['username'];
$password     = $_REQUEST['password'];

if (!isset($_REQUEST['firstName']) || !isset($_REQUEST['lastName']) || !isset($_REQUEST['emailAddress']) || !isset($_REQUEST['username']) ||
        !isset($_REQUEST['password'])) {
    echo "One or more parameters were not passed in correctly.";
}

require_once '../../../Chef.php';

// ENVIRONMENT VARIABLES
// The URL for the Chef Server
$server  = "mychefserver.com";
// The name used when authenticating
$client  = "pivotal";
// The location of the file which contains the client key
$key     = "../../../pivotal.pem";
// The version of the Chef Server API that is being used
$version = "12.0.2";

// Create a Chef object
$chef = new Chef($server, $client, $key, $version);

// API Request
$createUserRequest = $chef->get('/users/' . $username);

// Return JSON representation of value
// $encoded_role = json_encode($role_information);

// Decode a JSON string
// $decoded_role = json_decode($encoded_role, true);

echo json_encode($createUserRequest);

//echo json_encode($run_list_array);
?>

1 个答案:

答案 0 :(得分:0)

根据REST标准,这或多或少是正常的(参见this question)。

应该使用POST请求来创建用户,而不是更新它。因此,如果您对现有用户进行POST,则服务器不允许它(405代码的含义)。

在此处引用documentation

  

POST方法用于创建新用户。如果没有公钥   如果指定,将生成公钥和私钥   回。如果指定了公钥,则只有公钥   返回。

     

PUT方法用于更新特定用户。如果值不是   为PUT方法指定,Chef服务器将使用现有的   值而不是分配默认值。

因此,您的代码应该测试(使用GET)用户是否存在根据您的意愿选择POST或PUT。

编辑后,我会编写此代码而不是$chef->get(未经测试):

$createUserRequest = array("name"=>"$firtname $lastname")
$chef->post("/users/$username", json_encode($createUserRequest) )

我允许你扩展arry以包含其他信息,json_encode应该在关联数组上执行,所以你只需要将正确的json发送到chef服务器。