phpipam / phpipam-api-clients

Collection of API clients for phpipam API
43 stars 34 forks source link

return HTTP/1.1 400 Bad Request #17

Open dowdot opened 6 years ago

dowdot commented 6 years ago

I use curl exec is ok , but use api-clients call will return HTTP/1.1 400 Bad Request error... who can help me....

[root@weather IPAM]# curl -X GET http://ipam.xxx.xxx.xxx/api/ipam/user/ --header 'token:h==kas7bR$eX__2TgHKyW0oO'

{"code":200,"success":true,"data":{"expires":"2018-04-14 20:24:39"},"time":0.043}

[root@IPAM]# php example.php 
* About to connect() to ipam.xxx.xxx.xxx port 80 (#0)
*   Trying 127.0.0.1...
* Connected to ipam.xxx.xxx.xxx (127.0.0.1) port 80 (#0)
> POST /api/ipam/user/ HTTP/1.1
User-Agent: phpipam-api php class
Host: ipam.xxx.xxx.xxx
Accept: */*
Authorization: Basic YXBpOm5jaHVjY25ldDIxNw==
Content-Length: -1
Content-Type: application/x-www-form-urlencoded
Expect: 100-continue

< HTTP/1.1 400 Bad Request
< Date: Sat, 14 Apr 2018 06:26:55 GMT
< Server: Apache/2.4.6 (CentOS) OpenSSL/1.0.1e-fips mod_fcgid/2.3.9 PHP/5.6.33
< Connection: close
< Content-Type: text/html; charset=iso-8859-1
< 
* Closing connection 0
<?php
#api-config.php
# set error reporting
error_reporting(E_ALL ^ E_NOTICE ^ E_STRICT);

# api params
$api_url    = "http://ipam.xxx.xxx.xxx/api/";     // server url
$api_app_id = "ipam";                     // application id
$api_key    = false;                       // api key - only for encrypted methods, otherwise must be false

# set username / password for authentication, not needed for encrypted communications
$api_username = "api";
$api_password = "testpassword";

# save token or not ?
#   false => dont save, check each time
#   filename => will save token to filename provided
$token_file = false;

# set result format json/object/array/xml
$result_format = "json";

?>
<?php
#example.php
# include config file and api client class file
require("api-config.php");
require("class.phpipam-api.php");
# init object with settings from
$API = new phpipam_api_client ($api_url, $api_app_id, $api_key, $api_username, $api_password, $result_format);

$API->set_debug (true);

$API->execute ("GET", "user", array(), array(), $token_file);

#  get result
$result = $API->get_result();

# print result
print_r($response_headers);
// print_r($result);
print_r(json_decode($result, true));
?>
litebito commented 6 years ago

Which message do you get back from the API? try a var_dump($result) to see what the "message" is

dowdot commented 6 years ago

The token can't not write to token file. I am trace code, finding an key point

private function curl_authenticate () {
        // Get cURL resource
        $c_auth = curl_init();

        // set default curl options and params
        curl_setopt_array($c_auth, array(
                CURLOPT_RETURNTRANSFER => 1,
                CURLOPT_URL => $this->api_url.$this->api_app_id."/user/",
                CURLOPT_HEADER => 0,
                CURLOPT_VERBOSE => $this->debug,
                CURLOPT_TIMEOUT => 30,
                CURLOPT_USERAGENT => 'phpipam-api php class',
                // ssl
                CURLOPT_SSL_VERIFYHOST => 0,
                CURLOPT_SSL_VERIFYPEER => 0,
                CURLOPT_HTTPHEADER => array('Authorization: Basic '. base64_encode($this->api_username.":".$this->api_password)),
                CURLOPT_POST => true,
            )
        );

The problem seems use CURLOPT_POST => true but not setting CURLOPT_POSTFIELDS , I am set CURLOPT_POSTFIELDS => 0 The problem no longer occurs. Now, token can write to token file. This may be a different issue with the curl version.

But dump $result still empty. I try to solveing........

gilcot commented 6 years ago

I'm facing also the same HTTP/1.1 400 Bad Request issue :face_with_head_bandage: with the headers Expect: 100-continue and Content-Lenght: -1 ...but without token file use.

@dowdot catch it, but 0 (that's making a Content-Lenght: 1 and I can see upload completely sent off: 1 out of 1 bytes then * additional stuff not fine transfer.c:1042: 0 0 etc.) here is a bad/strange request; I'm set CURLOPT_POSTFIELDS => '' (that's making a Content-Lenght: 0 and now only * additional stuff not fine transfer.c:1042: 0 0 etc.)

Note that we can achive that later result without setting CURLOPT_POSTFIELDS but adding 'Content-Lenght: 0' into the array passed to CURLOPT_HTTPHEADER)

Many many thanks.