AnsweredAssumed Answered

Rest API not create new record in 7.7

Question asked by Autchara Chaiprom on Oct 26, 2016
Latest reply on Oct 26, 2016 by Autchara Chaiprom

I have problem I cannot create record via Rest API after I upgrade to 7.7

In sugar we have example for 7.6 it work fine with PUT POST etc.

After I upgrade it need to change some code in call function and it work with PUT but POST not work.

No response return blank page with 200 OK.

Could you help me to find out why my code not working for POST method?

 

<?php

$base_url = "http://localhost/sugarcrm/htdocs/rest/v10'";
$username = "admin";
$password = "admin";

/**
* Generic function to make cURL request.
* @param $url - The URL route to use.
* @param string $oauthtoken - The oauth token.
* @param string $type - GET, POST, PUT, DELETE. Defaults to GET.
* @param array $arguments - Endpoint arguments.
* @param array $encodeData - Whether or not to JSON encode the data.
* @param array $returnHeaders - Whether or not to return the headers.
* @return mixed
*/

function call(
    $url,
    $oauthtoken='',
    $type='GET',
    $arguments=array(),
    $encodeData=true,
    $returnHeaders=false
)
{
    $type = strtoupper($type);

    if ($type == 'GET')
    {
        $url .= "?" . http_build_query($arguments);
    }

    $curl_request = curl_init($url);

    if ($type == 'POST')
    {
        curl_setopt($curl_request, CURLOPT_POST, 1);
    }
    elseif ($type == 'PUT')
    {
        curl_setopt($curl_request, CURLOPT_CUSTOMREQUEST, "PUT");
    }
    elseif ($type == 'DELETE')
    {
        curl_setopt($curl_request, CURLOPT_CUSTOMREQUEST, "DELETE");
    }

    curl_setopt($curl_request, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
    curl_setopt($curl_request, CURLOPT_HEADER, $returnHeaders);
    curl_setopt($curl_request, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl_request, CURLOPT_FOLLOWLOCATION, 0);
    // Add more
    curl_setopt($curl_request, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));

    if (!empty($oauthtoken))
    {
        // Remove ,"Content-Type: application/json" after {$oauthtoken}
        $token = array("oauth-token: {$oauthtoken}");
        curl_setopt($curl_request, CURLOPT_HTTPHEADER, $token);
    }

    if (!empty($arguments) && $type !== 'GET')
    {
        if ($encodeData)
        {
            //encode the arguments as JSON
            $arguments = json_encode($arguments);
        }
        curl_setopt($curl_request, CURLOPT_POSTFIELDS, $arguments);
    }

    $result = curl_exec($curl_request);

    if ($returnHeaders)
    {
        //set headers from response
        list($headers, $content) = explode("\r\n\r\n", $result ,2);
        foreach (explode("\r\n",$headers) as $header)
        {
            header($header);
        }

        //return the nonheader data
        return trim($content);
    }

    curl_close($curl_request);

    //decode the response from JSON
    $response = json_decode($result);

    return $response;
}

//Login - POST /oauth2/token

$url = $base_url . "/oauth2/token";

$oauth2_token_arguments = array(
    "grant_type" => "password",
    //client id/secret you created in Admin > OAuth Keys
    "client_id" => "blablabla",
    "client_secret" => "blablablakey",
    "username" => $username,
    "password" => $password,
    "platform" => "other_app"
);

$oauth2_token_response = call($url, '', 'POST', $oauth2_token_arguments);

//Create record - POST /<module>/

$url = $base_url . "/Calls";

$record_arguments = array(

    "name" => "test call",
    "description" => "Description of call",
);

$record_response = call($url, $oauth2_token_response->access_token, 'POST', $record_arguments);

echo "<pre>";
print_r($record_response);
echo "</pre>";

?>

 

Thanks,

May

Outcomes