How can I get Request Parameter of this below code in JSON Format For Logging it into Db - laravel

$result = Http::withHeaders([
'Authorization' => config(''),
])->post($endPoint, [
"username" => '',
"password" => '',
// "username" => $user->username,
// "password" => $user->password,
"days" => 7
]);
In this code I want to save the request and request parameter in JsonFormat for logging into mysql db.

Laravel testing
https://laravel.com/docs/master/testing
or
Postman offers a sleek user interface with which to make HTML requests, without the hassle of writing a bunch of code just to test an API's functionality
https://www.postman.com/

Related

Unable to generate pdf from HTML using Doppio

I am trying to use https://doc.doppio.sh/guide/cookbook/protected-url.html to convert a html to pdf. On this one, since my url is password protected I am having it difficult to print the page.
My code:
$client = new \GuzzleHttp\Client();
$response = $client->post('https://api.doppio.sh/v1/render/pdf/sync', [
'headers' => [
'Accept' => 'application/json',
'Authorization' => 'Bearer apikey',
'Content-Type' => 'application/json'
],
'json' => [
'page' => [
"pdf" => [
"printBackground" => true
],
"goto" => [
"url" => 'https://example.com/admin/property'
],
"authenticate" => [
"username" => 'user#domain.com',
"password" => 'password'
],
]
]
]);
$responseBody = $response->getBody();
This is printing the page, however instead it is printing the login page. Its because when user is not authenticated, the system will redirect user to the login page.
Here, it has also mentioned about the https://doc.doppio.sh/guide/cookbook/protected-url.html#using-cookies cookie methods,however, I don't know what cookie should I pass on which name.
Your problem is more related to how does the login/auth works on the page you try to convert.
The "authenticate" prop you use here is related to HTTP Basic Authentication and I am not sure its what the page you want to access is using.
If you manually login into the page, check your browser, what is happening ? Do you see cookies ?

Laravel not making Guzzle HTTP POST request correctly

I am working on a megento integration and trying to get the admin access token by making a post request with form data. I tested the route out on Postman and it worked correctly:
However, when I tried to implement the same request in Laravel with Guzzle Http Client, it seem just cannot make the request properly as if the form data post body is not being recognized, and it keeps showing me errors saying the field values are required. Here is my request:
$client = new \GuzzleHttp\Client();
$response = $client->post($request['magento_domain'] . '/rest/V1/integration/admin/token', [
'form_params' => [
'username' => $magento_admin_username,
'password' => $magento_admin_password
], [
'Accept' => 'application/json',
'Content-Type' => 'application/json'
]
]);
and then this is the error I keep getting:
Update: I had also tried the request like this, it throws the same error:
$response = $client->post($request['magento_domain'] . '/rest/V1/integration/admin/token', [
'form_params' => [
'username' => $magento_admin_username,
'password' => $magento_admin_password
]
]);
I would appreciate any help!
The "Content-Type: application/json" request header is incorrect when sending a form-data body.
Just remove it, Guzzle automatically adds the correct Content-Type when using "form_params". Just JSON is wrong because the body is obviously not JSON.
I am using a JSON request in production successfully:
$res = $this->client->request('POST', 'https://.../rest/V1/integration/admin/token', [
'headers' => [
'Accept' => 'application/json',
'content-type' => 'application/json'
],
'json' => [
'username' => config('app.shopUser'),
'password' => config('app.shopPw')
]
]);
Or try using "multipart" instead of "form_params" - this should send a multipart/form-data request which is what Postman means with "form-data".
"form_params" is equivalent to "x-www-form-urlencoded".

Laravel passport generate user access and refresh token with only user id

Currently i would like to generate user access and refresh token after registering a user inorder to automatically login a user after registrations
Currently am doing this.
$user = User::create(//user detaisl) //this creates a user
$guzzle = new \GuzzleHttp\Client([
'base_uri' => env("APP_URL"),
'defaults' => [
'exceptions' => false
]
]);
$response = $guzzle->post('/oauth/token', [
'form_params' => [
'grant_type' => 'password',
'client_id' => env("PASSPORT_CLIENT_ID"),
'client_secret' => env("PASSPORT_CLIENT_SECRET"),
'username' => $user->email,
'password' => $request->input("password"),
],
]);
return json_decode((string) $response->getBody(), true);
The above with guzzle works but i was wondering if there is a simpler way to simply generate access and refresh tokens without need to perform another guzzle http request by simply using the user id after create.
I would like this as guzzle sometimes fails to work especially on localhost during development continously hangs.
Is there another way?
Instead of using a guzzle request, you can call directly the controller method that handles the token route. Generally directly calling another controller method is a bit of a code smell. You could attempt to dive into the code to refactor this out if you wanted, but since you don't "own" the passport code, I wouldn't worry about it.
// Save off the original request object.
$originalRequest = app('request');
// Create a new request object for the token request.
$tokenRequest = \Illuminate\Http\Request::create('/oauth/token', 'POST', [
'grant_type' => 'password',
'client_id' => config('passport.password_client_id'),
'client_secret' => config('passport.password_client_secret'),
'username' => $user->email,
'password' => $request->input("password"),
]);
// Replace the current request with the new token request in the app container.
app()->instance('request', $tokenRequest);
// Call the access token controller method using the app container,
// which will auto inject the new request.
$response = app()->call('\Laravel\Passport\Http\Controllers\AccessTokenController#issueToken');
// Replace the token request in the container with the original request.
app()->instance('request', $originalRequest);
return $response;
A couple notes:
The $user->createToken() method creates personal access tokens, not password grant tokens. Personal access tokens cannot be refreshed.
I converted the env() calls to config() calls. You should avoid using the env() method outside of the config files. As soon as you cache your config, the env() calls will return null (for values only set in the .env file).

Guzzle Post Null/Empty Values in Laravel

I've been trying to work with Guzzle and learn my way around it, but I'm a bit confused about using a request in conjunction with empty or null values.
For example:
$response = $client->request('POST',
'https://www.testsite.com/coep/public/api/donations', [
'form_params' => [
'client' => [
'web_id' => NULL,
'name' => 'Test Name',
'address1' => '123 E 45th Avenue',
'address2' => 'Ste. 1',
'city' => 'Nowhere',
'state' => 'CO',
'zip' => '80002'
],
'contact' => [],
'donation' => [
'status_id' => 1,
'amount' => $donation->amount,
'balance' => $donation->balance,
'date' => $donation->date,
'group_id' => $group->id,
],
]
]);
After running a test, I found out that 'web_id' completely disappears from my request if set to NULL. My question is how do I ensure that it is kept around on the request to work with my conditionals?
At this point, if I dd the $request->client, all I get back is everything but the web_id. Thanks!
I ran into this issue yesterday and your question is very well ranked on Google. Shame that it has no answer.
The problem here is that form_params uses http_build_query() under the hood and as stated in this user contributed note, null params are not present in the function's output.
I suggest that you pass this information via a JSON body (by using json as key instead of form_params) or via multipart (by using multipart instead of form_params).
Note: those 2 keys are available as constants, respectively GuzzleHttp\RequestOptions::JSON and GuzzleHttp\RequestOptions::MULTIPART
Try to define anything like form_params, headers or base_uri before creating a client, so:
// set options, data, params BEFORE...
$settings = [
'base_uri' => 'api.test',
'headers' => [
'Accept' => 'application/json',
'Authorization' => 'ajustneedatokenheredontworry'
],
'form_params' => [
'cash' => $request->cash,
'reason' => 'I have a good soul'
]
];
// ...THEN create your client,
$client = new GuzzleClient($settings);
// ...and finally check your response.
$response = $client->request('POST', 'donations');
If you check $request->all() at the controller function that you are calling, you should see that were sent successfully.
For those using laravel, use this:
Http::asJson()

Unable to perform actions after login in codeception due to authentication failure [Not able to send tokens]

I'm working on automating testing using codeception. How do I perform action after login.
For better understanding I have posted my code below.
$I->amOnRoute('login');;
$I->sendPOST('/login', ['email' => 'un', 'password' => `enter
code here`'pwd']);
$I->seeResponseCodeIs(200);
Above lines of code works fine, but I get 401 authentication error when I continue with below code
$I->sendAjaxPostRequest('/adminproxy/create-entity',[
"name" => 123123123123,
"title" => 123123123,
"realm_id" => 1,
"description" => 123123123123,
"status" => 'ACTIVE',
"entity_type" => 'Workspace' ]);
It could be a problem that you mix actions of Laravel5 module and REST module.
Instead of using sendPOST, it could be better to submit a form.
$I->amOnRoute('login');
$I->submitForm('#login-form', ['email' => 'un', 'password' => 'pwd']);
$I->seeResponseCodeIs(200);

Resources