Laravel Api method - laravel

Hello I am trying to create my venues registration values from Laravel API (POST URL) and I am doing right when I check with postman and it shows me the success values and when I give the same routes controllers and whatever I need I give to the mobile app developer and when he tried to run the same thing which is obviously working fine it does not work there it shows
CLIENT PROTOCOL EXCEPTION I does not understand what to do
Any help will highly appreciated
org.apache.http.client.HttpResponseException: client protocol exception

If you used the guzzle http package, it is bundled with the CLientException error class which is an extension of the laravel RuntimeException. This is caused by a a wrong url fed into the guzzle http library.
for instance a
$http = new guzzleclient;
$response = $http->post('localhost/stack/overflow', [
'form_params' => [
"params" => "params"
],
]);
make sure you change the 'localhost/stack/overflow' url to your current server url, else it will throw errors.
I hope this helps.. Cheers

Related

Getting GET error while sending POST request to the POST route in Laravel API

Locally project runs fine. On the server getting this error while sending POST request to the POST route in Laravel 9 API.
"message": "The GET method is not supported for this route. Supported methods: POST."
My route from the api.php routes file:
Route::post('/userdata/create', [UserDataController::class, 'createAccount']);
My route from the routes list (from php artisan route:list):
POST api/v1/userdata/create .......... Api\V1\UserDataController#createAccount
Tried:
php artisan cache:clear
php artisan route:cache
php artisan route:clear
Didn't fixed it yet.
It happens cause you have forced your domain to HTTPS redirect and you are trying to use it as HTTP.
Your domain settings redirect your request to HTTPS but not to POST method.
If you look for postman logs/console you will find redirection of HTTP POST request to HTTPS GET request
Hence Error response by your server of
The solution to that problem was actually very simple:
I needed to send POST request to "https" url, and i was sending it to "http" url.
Still strange that server was complaining that GET method is used...
This problem happened to me recently where I was using .com instead of .net
that happen because laravel cannot return the error.
SOLUTION 1 :
you can use this inside your API post function :
try{
// example we have name field
$request->validate([
'name' => ['required','string','max:255']
]);
User::create([
'name' => $request->name
]);
return 'registered';
}catch(Exception $error){
// your error from validation will appear here
// dd($error->validator->messages());
}
SOLUTION 2 :
as alternative :
I found another method here at this link

How can I test the Api in Laravel via Postman?

I have a route like this:
Route::group(
[
'prefix' => 'v1', 'as' => 'v1.',
],
static function () {
Route::get('test', 'Api\TestController#test')->name('test');
///
How can I refer to this route so that it would work? I am trying to call this route in Postman http://localhost/api/v1/test but I get a 400 error
You didn't provide enough information but check these just to be sure
Make sure these routes are listed among the api routes and not the web
If you're using artisan to serve your project, then ensure you're using the correct port in postman too eg http://127.0.0.1:8000/api/v1/test
(Pretty obvious) Make sure you're sending a get request from postman
Check the function too to know if there's any problem. You could do an early return of a simple json response in the beginning of the function to make sure the route accessed the function at all

Load test Laravel application with jmeter

I am trying to load test a Laravel application, however I'm stuck at the very beginning. I am attempting a login with a POST request, but I'm always getting response code: 419. I have googled and asked around a bit to no avail.
So far, I have extracted the xsrf token from the GET request and am trying to append it as a header to the POST request. I'm not sure if I'm doing it correctly, however.
That's what my header manager looks like, I looked at the post request through dev tools when doing it manually and I tried to replicate it.
I really can't tell what I'm doing wrong. I don't think I can fix this by using a different tool.
For web routes you need to Disable CSRF, or put csrf on the jmeter.
For API routes you need to disable rate limiter. Just go to app/Http/Kernel.php and comment throttle:60,1 line
protected $middlewareGroups = [
...
'api' => [
// 'throttle:60,1',
],
];
Maybe you should disable these on test environment only, in most cases in the production these feature is needed.

Vuejs Axios POST request gets HTTP422 error from Laravel backend

Hi I am using Vuejs in the frontend and Laravel in the backend. The role of Laravel is handling the API only. The frontend and backend are separated, i.e. I am not using Vuejs in Laravel's resource/js folder.
Now I am sending Axios POST request from Vuejs to Laravel. All the form input values are prevalidated using HTML5 required attribute. And when I console.log the request data, it shows all the fields filled.
In Vue file:
const data = {
name: this.name,
gender: this.gender,
mobile_no: this.mobile_no,
image: this.userImage
};
console.log("Request data . . . .", data);
const response = await this.axios
.post(`${this.AppURL}/admin/user/create`, data, {
headers: {
"Content-Type": "multipart/form-data"
}
})
.then(() => {
console.log("Success. . . . ")
alert("Successfully Driver Added");
})
.catch(error => console.log(error));
And in Laravel, the request is passed through some validation. It's a simple validation to check if all the fields are filled.
I am also using JWTAuth package for the authentication, and the token is generated by it.
It's too much code to write them all the way down here. But I am sure you can understand what I mean.
What I am getting as a response is this
POST http://localhost:8000/api/admin/user/create 422 (Unprocessable Entity)
The actual result I am expected to get is either success or some errors that is according to some if conditions in validation or token check.
I tried to figure out where this error might come from. What I think at the moment is this could be due to the absence of csrf_token in the POST request. As I'm sending the request outside Laravel, csrf_token is missing in the form. I am not 100% sure though about this.
So my question is:
How can I include csrf_token in Axios POST request, when I send it from outside Laravel.
If this 422 error is not related with csrf_token, what could be causing this? Any previos experiences like min? and any solutions for this?
Thanks in advance for your help.
Please, modified catch block as #Jack suggested:
.catch(error => {
console.log("ERRRR:: ",error.response.data);
});
Now you can get errors and handle errors in the catch block.
.catch(function (error) {
console.log(error.response.data.errors);
});
please use this code I hope it work's.
I was also facing the same issue, i think it is due to some Headers missing in your Api request from vue.js. here some tips which may helps you to solve this issues.
Make sure that you are protecting your Api Routes or not(by sanctum or something else). If you are protecting , then make make sure that you are sending authentications token in headers.
Second make sure that your request(axios or jwt) should contained valid data, if your are sending images or files etc make sure how can we send them.
First, get request and check in laravel by dd($erquest->all()); if you are geeting data then validate, it is possible that laravel request doesnt contained your sending data..
These errors may be caused due to follow reasons, ensure the following steps are followed.
To connect the local host with the local virtual machine(host).
Here, I'am connecting http://localhost:3001/ to the http://abc.test
Steps to be followed:
We have to allow CORS, placing Access-Control-Allow-Origin:* in header of request may not work. Install a google extension which enables a CORS request.
Make sure the credentials you provide in the request are valid.
Make sure the vagrant has been provisioned. Try vagrant up --provision
this make the localhost connect to db of the homestead.
Just click on the preview tab within network section in the dev tool, you are going to see the actual error message.

Paypal's IPN Simulator using Laravel 5

I'm about to go mental with this problem, I'm implementing an IPN system in my app and started doing tests now using Paypal's IPN Simulator.
When I try to send an IPN simulation, it just gives the following error:
We're sorry, but there's an HTTP error. Please try again.
First thought - Paypal's service was down - Tested wrong since if I create a blank page and send an IPN message to http://myDNS.com/blankpage.php it is able to send it.
Second thought - Problem with routes - which I think it's not the problem either:
Here's my IPN Listener at the PurchaseController.php:
public function completed()
{
//FAHIM's Paypal IPN Listener
$ipn = new PaypalIPNListener();
$ipn->use_sandbox = true;
$verified = $ipn->processIpn();
$report = $ipn->getTextReport();
Log::info("-----new payment-----");
Log::info($report);
if ($verified) {
if($_POST['address_status'] == 'confirmed'){
//sucess
}
}
}
In routes.php :
Route::post('purchase/completed/', array('as' => 'purchase.completed', 'uses' => 'PurchaseController#completed'));
Is there any known problems associated with IPN Simulator and Laravel?
Thank you in advance.
Looks like I found the answer!
The problem was that a tokenMismatchException was being thrown whenever Paypal tried to send the POST information.
For people with the same problem, here's the solution:
Add an exception into the VerifyCsrfToken.php Middleware, so that the exception URI won't need the CsrfToken verification:
In my case, it looks something like this:
protected $except = [
'purchase/completed'
];
I'm working with Laravel 5, so please keep in mind that it might be slightly different in lower versions.

Resources