I'm trying to test an action which has email as a parameter.
Here is code:
$client = static::createClient();
$crawler = $client->request('GET', '/api/register/emailverification/',
array('email' => 'email#gmail.com'));
$response = $client->getResponse();
$this->assertEquals(200, $response->getStatusCode());
But it returns 404 error.
PS: when I test this in the url it works fine!
Related
Hi i want to consume a service and i use laravel 5.x with guzzle with this code i can send request and i use the correct api-key but i always obtain 403 forbidden....
public function searchP(Request $request) {
$targa = request('targa');
$client = new \GuzzleHttp\Client();
$url = 'https://xxx.it/api/xxx/xxx-number/'.$targa.'/xxx-xxxx';
$api_key ='xxxxxcheepohxxxx';
try {
$response = $client->request(
'GET',
$url,
['auth' => [null, $api_key]]);
} catch (RequestException $e) {
var_dump($e->getResponse()->getBody()->getContent());
}
// Get JSON
$result = $response->json();
}
Why? I cannot understand
In postman i write in the AUTHORIZATION label this
key : x-apikey
value: xxxxxcheepohxxxx
Add to header
and it works.
i also tried this
.... try {
$response = $client->request('GET',$url,[
'headers' => [
'x-apikey', $api_key
]
]);
} catch .....
but doesn't work
Thx
it should be this, you have a typo
.... try {
$response = $client->request('GET',$url,[
'headers' => [
'x-apikey'=> $api_key
]
]);
} catch .....
How to get the URI/URL of the request sent, from the response?
<?php
// Create a client with a base URI
$client = new GuzzleHttp\Client(['base_uri' => 'https://example.com/api/']);
// Send a request to https://example.com/api/test
$response = $client->request('GET', 'test');
// I want the following line to print 'https://example.com/api/test'
var_export( $response->getUrl() );
Note: I want something like the last line of the above snippet to work.
I'm trying to make a token request using guzzle and receive an error "400 Bad Request` response: {"error":"invalid_client"}". I can make the same request with cURL and HTTP_Request2 with no problem.
<?php
require 'vendor/autoload.php';
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Psr7\Request;
session_start();
if(isset($_GET['code'])){
$code = $_GET['code'];
$encodeB64 = base64_encode('{clientID}:{clientSecret}');
$client = new GuzzleHttp\Client();
$response = $client->request('POST', 'https://identity.reckon.com/connect/token',[
['headers' => ['Content-Type' => 'application/x-www-form-urlencoded'],['Authorization' => 'Basic '.$encodeB64]],
['body' => ['grant_type' => 'authorization_code'],['code' => $code],['redirect_uri' => '{redirectURI}']]
]);
$body = $response->getBody();
echo $body;
}
These are the details of how to make a token request with this API:
URL: https://identity.reckon.com/connect/token
Type: POST
Body: grant_type=authorization_code&code={code}&redirect_uri={redirect url}
Headers:
Content-Type = application/x-www-form-urlencoded
Authorization: Basic{client id:client secret encoded in base64}
Not sure where I'm going wrong.
I have worked it out. The answer was the following:
<?php
require 'C:/Users/Shane/vendor/autoload.php';
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Psr7\Request;
session_start();
if(isset($_GET['code'])){
$code = $_GET['code'];
$encodeB64 = base64_encode('{client id}:{client secret}');
$authbody = 'grant_type=authorization_code&code='.$code.'&redirect_uri={redirect url}';
$client = new GuzzleHttp\Client();
$response = $client->request('POST', 'https://identity.reckon.com/connect/token',['headers' =>
['Content-Type' => 'application/x-www-form-urlencoded','Authorization' => 'Basic '.$encodeB64],
'body' => $authbody]);
$body = $response->getBody();
echo $body;
I have recently gone through {"error":"invalid_client"} with Guzzle, the error actually tells you specifically if something wrong with clientId or clientSecret. In my case I had first letter of clientSecret capitalized. It took a while to figure it out.
I'm trying to fetch orders data on my InfusionSoft account. I can do it using the command line but the Guzzle code gives me 401 Unathorized. I suppose I'm doing something wrong and not able to pass the params correctly. Can someone help?
Here's what works from the command line:
curl -G --data "access_token=abcdefgh12345678" https://api.infusionsoft.com/crm/rest/v1/orders?limit=1&offset=100&order_by=id
And here's the (supposedly) equivalent code from PHP:
$token = 'abcdefgh12345678';
$requestBody = array('access_token' => $token);
$url = 'https://api.infusionsoft.com/crm/rest/v1/orders?limit=1&offset=100&order_by=id';
$client = new \GuzzleHttp\Client();
$response = $client->request('GET', $url, array(
'form_params' => $requestBody
));
$response = (string) $response->getBody();
You are sending a GET request, and a GET request cannot contain a body.
curl uses --data according to the request method, so for GET it adds the access token to the URL as a GET-parameter. So should you.
I'm going to write some Symfony2 UnitTests (derived from Symfony\ Bundle\ FrameworkBundle\ Test\ WebTestCase) to test ajax controllers, similar to this How to get Ajax post request by symfony2 Controller.
My big problem is to get the parameters into the "request" bag of the request, not into the "parameter" bag. Similar to the upper example the method in the controller looks like this:
public function ajaxAction(Request $request)
{
$data = $request->request->get('data');
}
But if i do a var_dump of the $request, the paramaters i supply in the WebTestCase do not appear in $request->request, but in $request->parameter. Let's say this is the portion of code in my webtestcase:
....
$client = static::createClient();
$client->request('POST', '/ajax/blahblah', ... ?????);
I already tried supplying the parameter(s) directly within the url as
/ajax/blahblah?data=whocares
I tried specifying the parameter within an array
$client->request('POST', '/ajax/blahblah', array('data' => 'fruityloops'));
But nothing worked. Any chance to get this running?
Thanks in advance
Hennes
After you make the request, you need to get the response. Try this:
$client = static::createClient();
$client->request('POST', '/ajax/blahblah', array('data' => 'fruityloops'));
$response = $client->getResponse();
$this->assertEquals(200, $response->getStatusCode());
//convert to array
$data = json_decode($response->getContent(true), true);
var_dump($data);
$this->assertArrayHasKey('your_key', $data);