Laravel 9 Request Always Blank When Update Data - laravel

I am using laravel 9 and Passport, I can't update data neither PUT nor PATCH method is working.
Here is my route api.php
Route::group(['middleware' => 'auth:api'], function() {
Route::get('logout', [AuthController::class,'logout']);
Route::resource('user',UserController::class);
Route::resource('/admin/post', PostController::class);
});
all another protected routes is ok, but when i Put or patch to localhost:8000 the request is always blank even the id is correct, for example in localhost:8000/api/admin/post/1 and in PostController update(Request $request,$id) when i dd($request, $id) will be "null",5
my request
curl --request PATCH \
--url http://localhost:8000/api/admin/post/5 \
--header 'Accept: application/json' \
--header 'Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJhdWQiOiIxIiwianRpIjoiZDU3MjhjZjZlMGVjMTVmMmQ1ZTJhYzFkMDU3MGQ0YzdhMDBlZmFhMDBjNWY2NDI1Yjk5MjY5YTFiZDNjNDE3Y2MxMmJmMjcwY2FkMjI0ZWUiLCJpYXQiOjE2NjA1MDMxNzYuNTUxMDk2LCJuYmYiOjE2NjA1MDMxNzYuNTUxMDk5LCJleHAiOjE2OTIwMzkxNzYuNTI5MTI0LCJzdWIiOiIxIiwic2NvcGVzIjpbXX0.VvYis4iH3pUNLp1Zz4UOC9lQ6h4Gpi4xA44BA4rd7iQe-Z5IowjEwMkkYceuq8sZJAnbp9xh-li_zYXq-tEFakN9gKm3-hxr6gE8sqRj4WCSnVOu_KoqSKWX_FVlDW_IEhIfzGQdeIIRDgXuyz5E6dXxpqqtEsOiRY0KNKxjKRIG7gz0D0CO4lzTRkbly8nCG-CeBBYYS_jXonG4comxPk6eWnbDkw4yNBtdHQ9HK-E92PixbeMW13JPZ5Iiu-JmhLfOSSLrUqa_bJNW7WmbPTsBiyNZyRZ8kEYVbeVvKpMl37HoIDvJnvS9iydLW20Zb7KQkFEoI7bFS7jJdySXXPWIHqnqnFv4u0P6ko1rlLt5PCzyitIX_TdxvXYSaxfUQJMf9SPCG2P0Juxhhcsym8ob70VskVIwSCIGgvbi96JNKBhIhR9bJUX6eEhEOWeWB7jBRaquevTtpTBAm2qbGJPv4Crx2DksFHIuBOxCD33F-MFOSxvK5K2t-NTOofic2cOziYLSgVSrPYy2HHdnriWoAvZynUM2hDYKntzG8x3VYqswYkXwONtyQZpGAGBnDcZq9Fa2YDV6Poqwy_RdeCY6wlyF_1RI6unOgpDYm6GtTE4n51jijNx_kTm9s7F48CHva4aGNATQPoRYuVSWdOZo5VcgkKMfI04np4fQTeY' \
--header 'Content-Type: multipart/form-data' \
--form 'title=mau diganti' \
--form 'description=Sebuah tutorial untuk membunuh polisi' \
--form 'content=hehe' \
--form 'tags=[1,2]' \
--form 'categories=[1]' \
--form 'thumbnail=#C:\Users\Bloop\Pictures\anak kak risti.jpg' \
what is wrong?

Add _method=PUT or _method=PATCH in your body, and use curl --request POST instead.
The PUT and PATCH methods aren't allowed in HTML forms, so Laravel uses this workaround instead.
From the Laravel Docs:
HTML forms do not support PUT, PATCH, or DELETE actions. So, when defining PUT, PATCH, or DELETE routes that are called from an HTML form, you will need to add a hidden _method field to the form. The value sent with the _method field will be used as the HTTP request method

Chose POST method and add PUT or Patch to your request body

Related

How to add Supabase Order by in get request using Postman

I need to add order in supabase result while calling the supabase bash in postman.
I am doing same in flutter like below
Future getPropertiesFromBirmingham() async {
var response = await client
.from('properties')
.limit(10)
.order('created_at', ascending: false);
return response;
}
Need to use same in postman but not getting anything on this.
I didn't find much on this topic, Tried using order in params but it didn't work.
You can get examples of cURL requests directly in Supabase API docs.
You can also check for more documentation directly in the PostgREST website.
curl 'https://supabase_project_ref.supabase.co/rest/v1/properties?limit=10&order=created_at.desc' \
-H "apikey: SUPABASE_KEY" \
-H "Authorization: Bearer SUPABASE_KEY"
You can also control where the NULLs will be (if any) by adding either of the following:
nullslast
nullsfirst
curl 'https://supabase_project_ref.supabase.co/rest/v1/properties?order=created_at.desc.nullslast' \
-H "apikey: SUPABASE_KEY" \
-H "Authorization: Bearer SUPABASE_KEY"

How to send json using faraday

I need to consume the linode api, in the description it says I have to send the data using curl like this
curl -H "Content-Type: application/json" \
-H "Authorization: Bearer $TOKEN" \
-X POST -d '{
"label": "asdfasdf"
"ssh_key": "asdfasdf"
}' \
https://api.linode.com/v4/profile/sshkeys
I tried using
http = Faraday::Connection.new 'https://api.linode.com/v4/profile/sshkeys'
http.authorization :Bearer, token
http.post('', { 'label' => 'adfadf', ..}.to_json)
But every request it says label and ssh_key required. I donĀ“t know how to send this particular request
anyone?
You need to specify the content-type as json in the header, if you're sending a json data.
http.post(
'',
{ 'label' => 'adfadf', ..}.to_json,
{ 'Content-Type' => 'application/json' }
)
Reference: https://lostisland.github.io/faraday/usage/

Laravel API: validate doesn't access the PUT request data in my API

Context
I am implementing a user information update using a PUT request in Laravel 8. I use Postman to send the request and see the results.
Expected behavior
My PUT request reaches the controller's function that is censed to update the authenticated user. The latter is updated successfully. So the validate call is executed succesfully and finds the data in the request.
Actual behavior
My PUT request reaches the controller's function that is censed to update the authenticated user. The latter is not updated successfully. In fact, the validate call is executed succesfully but doesn't find the data in the request.
Instead, data validation says:
{
"message": "The given data was invalid.",
"errors": {
"email": [
"The email field is required."
],
"name": [
"The name field is required."
]
}
}
The route & The request
Postman request
curl --location --request PUT 'https://XYZ/api/set_user_data' \
--header 'X-Requested-With: XMLHttpRequest' \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer 12|leRLA5yopCLIxe0oN9MMThctqD78iJDjZdZQkcgs' \
--data-urlencode 'email=XYZ#XYZ.FR' \
--data-urlencode 'name=test2'
It means in Postman terminology that no "Params" are sent, an Authorization Bearer token is sent, some Headers are sent and some Body's x-www-form-urlencoded data are sent.
API Route
In routes/api.php:
Route::put('/set_user_data', [UserController::class, 'setUserData'])->name('set_user_data');
UserController::setUserData:
public function setUserData(Request $request) {
if(!Auth::check()) {
return 'unauthenticated.';
}
$request->validate([
'email' => 'required|email',
'name' => 'required|string'
]);
// ... update user here but out of topic
}
What I tried to do... or to not do
Some Stackoverflow answers are: send a POST request and send in the body _method=PUT. I don't want to do this. I really prefer to send a PUT request. Because I am developing an API. It totally justifies the fact that I must use a PUT request and not a PUT one.
Some Stackoverflow answers are: use x-www-form-urlencoded not a simple form. It doesn't fix the problem; moreover it's already the case. Maybe it could help with images sending. (notice I don't want to send any image here).
Question
Why Laravel's validate don't find the data of my request and how to fix it?
You are sending the request as "Content-type: application/json", but you are not sending the body as valid JSON.
You are sending this:email=XYZ#XYZ.FR&name=test2
You should send this:
{"email":"XYZ#XYZ.FR", "name": "test2"}
a valid JSON object

How to pass form-data with rest-client

I am trying to translate a post request I send to a server with Postman to ruby RestClient.
In Postman, I send a (successful) POST request with a URL with params.
In my headers I have one parameter
Under Body I send more parameters under form-data. I tried adding these to the payload, which does not work:
I tried to translate it to the following call with rest-client
url = "https://..."
cookie = "..."
payload = {
payloadParam: "some param",
formDataParam: "form-data param"
}
headers = {
Cookie: cookie
}
Following their docs I then fired the following call:
data = RestClient.post(url, payload, headers)
So this doesn't pass the form-data params correctly. I have a feeling I am supposed to pass these parameters through something other than the payload, but I can't find any docs online explaining how. All I could find is a multipart example of loading an external file, but I don't have an external file.
How do I add form-data params to a POST request with Rest-Client?
My cURL structure looks like this:
curl -X POST \
'https://.../?payloadParam=xxxxxxxx' \
-H 'Accept: */*' \
-H 'Accept-Encoding: gzip, deflate' \
-H 'Cache-Control: no-cache' \
-H 'Connection: keep-alive' \
-H 'Content-Length: 55' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-H 'Cookie: xxxxxxxx' \
-H 'Host: xxxxxxxx.com' \
-H 'Postman-Token: xxxxxxxxxxxxxxx' \
-H 'User-Agent: PostmanRuntime/7.19.0' \
-H 'cache-control: no-cache' \
-d 'formDataParam=xxxxx&secondFormDataParam=yyyyy'

Parse Cloud - make a GET endpoint

I have made simple cloud function:
Parse.Cloud.define("clientsnames", function(request, response) {
var query = new Parse.Query("Clients");
query.select('name')
query.find({
success: function(results) {
response.success(results)
},
error: function() {
response.error("Failed");
}
})
});
But to call it I always must use POST
curl -s -X POST \
-H "X-Parse-Application-Id: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" \
-H "X-Parse-REST-API-Key: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" \
-H "Content-Type: application/json" \
-d "{}" \
https://api.parse.com/1/functions/clients
Which is, of course, a bad idea, and the request itself is obviously should be GET. But when I use GET with this request, I just get 404 (Document not found).
Is it possible to define a GET endpoint using Parse Cloud?
You can use Express js hosted on Parse CloudCode to setup custom endpoints
That way you can configure GET, POST without even using the Parse keys in the headers.
Head over to
https://www.parse.com/docs/cloud_code_guide#webhooks for detailed example

Resources