Laravel redirect to action with https - laravel-5

I am doing a redirect to a controller action in Laravel 5.8. The initial request is an https request, but the redirect for some reason creates an HTTP request. I don't quite understand why this happens. I know I can force HTTP to https, but I was hoping someone could help me understand why the redirect is an HTTP request.
return redirect()->action('Auth\LoginController#loginAjax',['email' => $request->email, 'password' => $request->password, 'register_form' => true]);

The basic Laravel redirect to a controller action or a named route, was always going to HTTP instead of https. To get around this, I did a full URL redirect, like this:
return redirect()->to(env('APP_URL').'/login/ajax?email='.urlencode($request->email).'&password='.urlencode($request->password).'&register_form=true');
This solved the issue for me.

Related

How to make SOAP request in Laravel?

This request requires Basic Auth: user and password.
Headers with two fields.
and xml body.
This API is tested working in Postman.
But I am not sure how to make this work in Laravel, I only work with JSON before.
Any suggestion how can I get started? or any package I can use?
You can use http post
$http=Http::withHeaders([
'Content-Type'=>'application/xml',
'SOAPAction'=>'balance'
])->post(url,$xmlBodyContent);
return response($http->body())
->withHeaders([
'Content-Type' => 'text/xml'
]);
for xml doc notation
Ref:https://www.php.net/manual/en/simplexml.examples-basic.php

Guzzle seems to strip password part from request url

I'm trying to make a Http request with a url like
https://{{api_key}}:{{api_password}}#{{store_name}}.myshopify.com/admin/api/{{api_version}}/products.json
But the Http response is throwing *** replacing the api_password, even if I hardcode it.
This is all done in a Laravel app and is failing with 400 Bad Request.
The request is working using postman.
Is the password actually being stripped out of the request, or is it just being hidden in the response?
Try using HTTP CLIENT
This is how it works:
Import use Illuminate\Support\Facades\Http;
Http::post(
"https://${api_key}:${api_password}#${store_name}.myshopify.com/admin/api/${api_version}/products.json",
[
"foo" => "bar"
]);
Http::post automatically converts headers to content-type: application/json.
HTTP CLIENT helps you to make less error and it also using guzzle.

Laravel api authenticates POST requests but returns 'Unauthenticated' with GET

I have setup a Route group to authenticate my API calls with a token.
POST requests work fine, valid token passes authentication and an invalid one returns "Unauthenticated" as expected.
When making a GET request I get "Unauthenticated" every time.
I'm making GET requests with the same api_token that I made the POST requests with and still get "Unauthenticated"
This is my route group
Route::group(['middleware' => ['auth:api', 'api']], function () {
Route::post('/', 'ApiPostController#store');
Route::get('/', 'ApiPostController#fetch');
});
I tried removing the POST route from the group, that didn't work. Tried changing the route (from / to /get) that didn't do anything either.
What is causing the GET requests to fail even though I'm sending a valid token?
The problem is you send api_token in body and the request body is different for GET and POST requests. Also sending tokens in the GET body is insecure, that's why you have to send tokens via headers.
If you are in Apache server you can try adding those line in .htaccess
RewriteEngine On
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]
you can also clear config and try
php artisan config:clear
you can add #csrf as well.

Laravel Passport throwing MethodNotAllowedHttpException exception when I try to access my routes via Insomnia REST API

This has happened way too often. I've cleared my local database by running php artisan migrate:fresh and after that, php artisan passport:install --force. For some obscure reason, I'm having this infernal error
MethodNotAllowedHttpException
when I try to access my auth:api routes only, since registration and login work.
These are my routes:
Route::middleware('auth:api')->group(function () {
Route::get('me', 'Api\UserController#details');
Route::get('profile/{username}', 'Api\UserController#getUsername');
Route::post('profile/{username}/trust', 'Api\TrustController#trust');
});
Route::post('register', 'Api\UserController#register')->name('register');
Route::post('login', 'Api\UserController#login')->name('login');
I am absolutely sure I am sending a GET request.
I know that this happens when I am not properly logged in, in other words, giving a correct token to the API, which is not the case now because I have logged in normally, and pasted the token in the header as usual.
What else? Before I refreshed my database it was working. After the refresh, this has started. Any ideas?
When you are sending this request laravel thinks that you are sending a web request instead of api request.
In order for laravel to know that its a api request set the headers:
Content-Type : application/json
Accept: application/json
On your postman request. Since based the 1 flag on your header I cann see that your headers are not set correctly.

Getting `502 Bad Gateway` when sending request with Guzzle PHP

I am having an App developed on Laravel 5.4. I used to have my project setup on Xampp on Windows and also under vagrant Machine.
The problem is that when I switched to MacOS and Valet, I have started getting problems with routes that were sending requests via Guzzlehttp package.
They imideatelly responded with 502 Bad Gateway
nginx/1.13.10, though I could access the same endpoints on 3rd party domains directly and even though I wrapped the code with exception handler.
How can I solve that problem?
I had a problem of 502 Bad Gateway using Guzzle. I fixed it by removing User-Agent from request.
$client = new \GuzzleHttp\Client();
$res = $client->request('GET', 'https://www.example.com', [
headers' => ['User-Agent' => null]
]);

Resources