ExternalResourceClient Returning no response

Hello All,

I am using ExternalResourceClient with GET method with Bearer token to replace curl coding. 

URL and AuthKey are working on Postman. Also, POST Method on ExternalResourceClient is working well.

But GET method on ExternalResourceClient is returning 200-ok but with empty brackets, while server returns JSON response with message and status fields.

Please guide,

Prasad

Fullscreen
1
2
$response = (new ExternalResourceClient())->get($url, ['Content-Type' => "application/json", 'Authorization' => 'Bearer ' . $dmsAuthKey ]);
var_dump($response->getBody()->getContents()); //returning {}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

  • Hey ,

    You might be running into issues in your environment.

    How about you try something very basic first (see the example below) and then once that works, you try your URL.

    Fullscreen
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    // ExternalResourceClient GET
    try {
    // Set timeout to 60 seconds and 10 max redirects
    $response = (new ExternalResourceClient(60, 10))->get('https://httpbin.org/get');
    } catch (RequestException $e) {
    $GLOBALS['log']->log('fatal', 'Error: ' . $e->getMessage());
    throw new \SugarApiExceptionError($e->getMessage());
    }
    $httpCode = $response->getStatusCode();
    if ($httpCode >= 400) {
    $GLOBALS['log']->log("fatal", "Request failed with status: " . $httpCode);
    throw new \SugarApiException("Request failed with status: " . $httpCode, null, null, $httpCode);
    }
    echo $response->getBody()->getContents();
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    SugarCRM | Principal Developer Advocate

  • Yes, Can see response 

    Fullscreen
    1
    { "args": {}, "headers": { "Host": "httpbin.org", "X-Amzn-Trace-Id": "Root=1-xx911461-243ebf43295ffb21388bexxf" }, "origin": "xxx.xxx.xxx.68", "url": "https://httpbin.org/get" }
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    And also tried 

    Fullscreen
    1
    $response = (new ExternalResourceClient(60, 10))->get($url, ['Content-Type' => "application/json", 'Authorization' => 'Bearer ' . $token]);
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    Postman is returning valid response with same $url and $token but ExternalResourceClient returning {}

  • what's the content-type you're getting back from the URL? try to get simple text and check the format.

    SugarCRM | Principal Developer Advocate

  • Hello All, 

    Code was missing "Accept:application/json" in header. So working code is 

    Fullscreen
    1
    $response = (new ExternalResourceClient(60, 10))->get($url, ['Accept' => "application/json", 'Authorization' => 'Bearer ' . $token ]);
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    Thanks for your assistance,

    Prasad