Guzzle returning a 404 on a valid URL - guzzle

I'm using Guzzle with CurlAuthPlugin to authenticate. When I run the code, I get
Client error response\ [status code] 404\ [reason phrase] Not Found\ [url] https:\/\/api.buto.tv\/v2\/video\/tag\/v2\/video\/tag\/
The code I'm using is:
$client = new Client(<URL>);
// Add the auth plugin to the client object
$authPlugin = new CurlAuthPlugin(<APIKEY>, 'x');
$client->addSubscriber($authPlugin);
$response = $client->get('v2/video/tag/')->send();
But the URL is perfectly valid a I can paste that in to a browser and it works fine
I've also tried:
$client = new Client('https://api.buto.tv');
$request = $client->get('v2/video/tag/');
$request->setAuth('user', 'pass');
$response = $request->send();
But I get the same error. I have output the URL it's requesting with echo $request->getUrl(); and if I copy and paste the URL in to a browser, the URL is fine

I think you may be missing a slash '/' after api.buto.tv, so the url is resolving to 'https://api.buto.tvv2/video/tag/' instead of 'https://api.buto.tv/v2/video/tag/'.
$client = new Client('https://api.buto.tv/');
$request = $client->get('v2/video/tag/');

Related

error 404: while deploying slim framework rest API on plesk shared hosting

I need help, while deploying slim framework on plesk shared hosting below mentioned issue is coming up.
actually it is working fine at local wamp server.
404 - File or directory not found.
The resource you are looking for might have been removed, had its name changed, or is temporarily unavailable.
here is my code.
index.php:
require '../vendor/autoload.php';
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With');
header('Access-Control-Max-Age: 86400');
$config = ['settings' => [
'addContentLengthHeader' => true,
'displayErrorDetails' => true
]];
$app = new \Slim\App($config);
require '../src/routes/hostUser.php';
require '../src/routes/user.php';
$app->run();
hostUser.php:
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
use Slim\Http\Message;
include_once 'Database.php';
var_dump($app);
$app->get('/loadprofile', function (Request $request, Response $response) {
$user_email = $_GET['emailid'];
$userid = $_GET['usertypeid'];
$database = new Database();
$db = $database->getConnection();
try {
$stmt = $db->prepare("SELECT * FROM user_details WHERE userid='".$userid."' AND email='".$user_email."'");
$stmt->execute();
$res = $stmt->fetch(PDO::FETCH_ASSOC);
return json_encode($res);
$db = null;
} catch(PDOException $e){
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
});
The control reached till var_dump($app) but it is not executing loadprofile service.
enter image description here

Zf2 - How to create request to external API with file upload

I have a Zf2 application that communicates with another Zf2 application through RestAPI calls.
I'm able to communicate between one to another using following code and exchange parameters:
//Prepare request
$request = new Request();
$request->getHeaders()->addHeaders(array(
'Content-Type' => 'application/x-www-form-urlencoded; charset=UTF-8'
));
$request->setUri($p_url);
$request->setMethod('POST');
$request->setPost(new Parameters($p_params));
$client = new Client();
//Send request
$client->resetParameters();
$response = $client->dispatch($request);
$data = json_decode($response->getBody(), true);
Now, I would like to do the same thing but with a multipart call: Json + files.
How can I do that?
I have tried several solutions from using setFileUpload method of client to writing headers parameters with content-type (multipart/form-data), content-disposition, ... without success.
Along my tests, I used Wireshark to check the request contents. Depending on the solution I tried, I fail in situation with "missing boundary" or HTTP error 405.
Thanks for your help.
Best
Finally, I found a solution
$this->_client->setUri($p_url);
$this->_client->setMethod('POST');
//Prepare for upload
$this->_client->setFileUpload($p_file, 'file');
//Set parameters along with file
$this->_client->setParameterPost($p_params);
//Send request
try {
$response = $this->_client->send();
} catch ( \Exception $ex ) {
}

Can't update jira issue with REST API

I'm trying to do this with powershell, but I'm getting 400 errors:
$RESTURL = 'https://mycomp.atlassian.net/rest/api/latest/issue/PROJ-61'
$body = '{"fields":{"assignee":{"name":"me"}}}'
$restcreds = [System.Convert]::ToBase64String(
[System.Text.Encoding]::ASCII.GetBytes(('me' + ":" + 'mypass123'))
)
$httpheader = #{Authorization = "Basic $restcreds"}
$restParameters = #{
Uri = $RESTURL;
ContentType = "application/json";
Method = "PUT";
Headers = $httpheader;
Body = $body;
}
Invoke-RestMethod #restParameters
If I remove "body" from the request and change it to a get I get back data successfully. It seems I just get modify the ticket
If you get a 400 (bad request), then that means something is wrong in your request body.
The response body will contain a more detailed error message and will make it clear what you have to fix.
Without the error message, I can only make a guess:
I'm not sure if setting assignee to "me" works, unless "me" really is the name of a user. What happens if you try with a complete username or if you use "key" instead of "name"?
The fact that a GET request works fine shows that your credentials are correct, so it's not an authentication problem.

Perl HTTP request : POST fails while GET succeeds

When I try to submit a POST request with Perl, it often ends in a 301 redirect to the homepage. Here is the code :
use LWP::UserAgent;
$ua = LWP::UserAgent->new;
# This does not work
my $url = 'http://www.opensubtitles.org/en/search2';
my $req = HTTP::Request->new(POST => $url);
$req->content('MovieName=the+terminator+(1996)');
# Pass request to the user agent and get a response back
print $req->as_string."\n";;
my $res = $ua->request($req);
if (!$res->is_success) {
print $res->status_line, "\n";
}
else {
print "Success in posting search\n";
}
In order to make it work, I have to manually use Firefox, go to the url (!). Then the script works. However, using a GET request works flawlessly :
# This works
my $url = 'http://www.opensubtitles.org/en/search2?MovieName=the+terminator+(1996)';
my $req = HTTP::Request->new(GET => $url);
Why is that ?
The site doesn't expect a POST to that URL, so it redirects you to back to the search page.
Firefox will use GET, not POST, if you just put the URL into the address line, that's why it works.

Getting Zend_Http Final URL

Making a simple request like:
$client = new Zend_Http_Client('http://example.org');
$response = $client->request();
How can I get the final URL after the redirects?
I have not seen a way in the documentation or the API docs unless I'm missing something.
Thanks in advance.
Zend_Http_Client update the last URL into Zend_Http_Client->uri property if there is redirect.
$sourceUrl = 'http://google.com';
$client = new Zend_Http_Client($sourceUrl);
$response = $client->request();
$finalUrl = $client->getUri()->__toString();
var_dump($sourceUrl);
// string(17) "http://google.com"
var_dump($finalUrl);
// string(25) "http://www.google.com:80/"
Not tested :
$response->getHeader('Location');
Get the last request from the client and then extract the headers.
$client = new Zend_Http_Client('http://webonyx.com');
$response = $client->request();
$lastHeaders = Zend_Http_Response::extractHeaders($client->getLastRequest());
// $lastHeaders['host'] will be your final redirected host