POST REST API : error 400 Bad Request

asked 2014-08-12 07:53:15 -0500

dinar agustin gravatar image

updated 2014-08-14 00:22:19 -0500

I've tried to create flavor from Openstack rest API, I already follow the instruction from the documentation, but it returns 200, and it say bad request, is there anything wrong from my json, this is sample of my json

private function set_flavorData() {
        $data = '{
                    "flavor": {
                        "name": "test_flavor",
                        "ram": 1024,
                        "vcpus": 2,
                        "disk": 10,
                        "id": "10"
                 }

            }';
         return $data;
    } 

    public function addFlavor(){

        $url = $this->create_uri('/'.$this->tenantId.'/flavors');
        $authData = $this->set_flavorData();

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));

        curl_setopt($ch, CURLOPT_POST, true);
        #curl_setopt($ch, CURLOPT_CUSTOMREQUEST,"POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $authData);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $this->authToken());

        $response = curl_exec($ch);
        curl_close($ch);

        $result = json_decode($response);
        return $result;
    }

and this is the response

stdClass Object ( [badRequest] => stdClass Object ( [message] => The server could not comply with the request since it is either malformed or otherwise incorrect. [code] => 400 )

)

I try to using curl command on terminal and that works, image description

but why on REST php that doesn't work? can anyone help me??? It doesn't work for another POST method in API too,

edit retag flag offensive close merge delete