API Request hangs without response when using Web->request()

92 views
Skip to first unread message

ved

unread,
Dec 20, 2017, 10:08:56 AM12/20/17
to f3-fra...@googlegroups.com
Hi there, 

I'm currently migrating some code to manage DNS records using Gandi's new Live DNS api. (was xml-rpc before, regular ReST now)

The issue I'm having is that when using Web->request() to run a POST request, there's no response output. In fact the process seems to hang and I have to "CTRL+C" to exit.

Here's the code using PHP's native cURL functions.
This works and returns the correct output. (success message from API)

$url = $this->host . 'domains/' . $this->domain . '/records';
$record
= json_encode([
   
'rrset_name' => $name,
   
'rrset_type' => $type,
   
'rrset_ttl'  => $ttl,
   
'rrset_values' => [$value]
]);
curl_setopt
($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt
($ch, CURLOPT_POSTFIELDS, $record);
curl_setopt
($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt
($ch, CURLOPT_HTTPHEADER, [
   
'X-Api-Key:' . $this->key,
   
'Content-Type: application/json',
   
'Content-Length: ' . strlen($record)
]);
$result
= curl_exec($ch);
return $result;


Now here's (what I assume) the equivalent using F3's Web class.
This works in the sense that the API does what it should, but it just hangs there until I break it and there's no output.

$url = $this->host . 'domains/' . $this->domain . '/records';
$record
= json_encode([
   
'rrset_name' => $name,
   
'rrset_type' => $type,
   
'rrset_ttl'  => $ttl,
   
'rrset_values' => [$value]
]);

$options
= [
   
'method' => 'POST',
   
'header' => [
       
'X-Api-Key:' . $this->key,
       
'Content-Type: application/json',
       
'Content-Length: ' . strlen($record)
   
],
   
'content' => $record
];
$result
= \Web::instance()->request($url, $options);
return $result;


Using Postman (the api testing app) the request also runs without any issues and returns the API success message.

Any ideas or is there any way to better debug or log the request in order to figure out what's going on?

Thanks.





ved

unread,
Dec 20, 2017, 11:50:48 AM12/20/17
to Fat-Free Framework
I've ran a few more tests and managed to figure out the issue which I'll explain here since it may require a slight update on F3 and may affect somebody else using some other APIs if they exhibit the same behavior.

When I echoed the $body variable right after the ob_get_clean() call on line 305 then I get the API's success message. If I echo it after the IF block that's right after that, then it hangs and doesn't do anything else.

Seeing that behavior, I then set the follow_location=false on the $options array and that seems to have solved it.

The API in question does indeed return a Location header although the HTTP code is: "HTTP/1.1 201 Created"

According to cURL's documentation about the follow_location parameter it states the following: 

A long parameter set to 1 tells the library to follow any Location: header that the server sends as part of a HTTP header in a 3xx response. The Location: header can specify a relative or an absolute URL to follow.

F3's code is not checking the status code for a 3xx and therefore keeps trying to follow links even after a 2xx code if follow_location is set.

So, in conclusion, maybe that IF condition should also check the response's http status code and only follow the Location if the code is one of the 3xx redirect codes.



xfra35

unread,
Dec 20, 2017, 4:35:11 PM12/20/17
to Fat-Free Framework
Well spotted ved!


Can you give it a try?

ved

unread,
Dec 21, 2017, 7:51:00 AM12/21/17
to Fat-Free Framework
Hey, that worked perfectly. Thanks!
Reply all
Reply to author
Forward
0 new messages