404 not found after changing header content-type from application/json to multipart/form-data laravel - laravel

public function customHeaders()
{
return [
'Authorization' => request()->header('Authorization'),
'Content-Type' => 'multipart/form-data',
'Access-Control-Allow-Credentials' => true,
];
}
public function post(Request $request)
{
$response = Http::withHeaders($this->customHeaders())->post($this->url, $request->all());
return response()->json($response->json(), $response->status());
}
I have no problem using application/json but I get 404 when using multipart/form-data

public function customHeaders()
{
return (
'multipart' => [
'Authorization' => request()->header('Authorization'),
'Content-Type' => 'multipart/form-data',
'Access-Control-Allow-Credentials' => true,
'Content-Disposition' => 'multipart',
'name' => 'multipart'
],
'form-data' => [
'Authorization' => request()->header('Authorization'),
'Content-Type' => 'application/json',
'Access-Control-Allow-Credentials' => true,
'Content-Disposition' => 'form-data',
'name' => 'form-data'
]
);
}

Related

Dropzone.js vuejs laravel message: "Undefined index: width"

When I select a file it is uploaded to the public/upload-images folder. In the laravel Controller the validation fails because the params are not included in the request. When I dump and die. This is what I get. As you can see... the params are missing. Below that is the Dropzone setup.
echo '<pre>';
print_r(request()->all());
echo '</pre>';
die();
$data = request()->validate([
'image' => '',
'width' => '',
'height' => '',
'location' => '',
]);
$image = $data['image']->store('user-images', 'public');
$userImage = auth()->user()->images()->create([
'path' => $image,
'width' => $data['width'],
'height' => $data['height'],
'location' => $data['location'],
]);
return new UserImageResource($userImage);
(
[image] => Illuminate\Http\UploadedFile Object
(
[test:Symfony\Component\HttpFoundation\File\UploadedFile:private] =>
[originalName:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 20220406_125155.jpg
[mimeType:Symfony\Component\HttpFoundation\File\UploadedFile:private] => image/jpeg
[error:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 0
[hashName:protected] =>
[pathName:SplFileInfo:private] => /tmp/phpdAwZvd
[fileName:SplFileInfo:private] => phpdAwZvd
)
)
props: [
'imageWidth',
'imageHeight',
'location',
],
data: () => {
return {
dropzone: null,
}
},
mounted() {
this.dropzone = new Dropzone(this.$refs.userImage, this.settings);
},
computed: {
settings() {
return {
paramName: 'image',
url: '/api/user-images',
acceptedFiles: 'image/*',
params: {
'width': this.imageWidth,
'height': this.imageHeight,
'location': this.location,
},
headers: {
'X-CSRF-TOKEN': document.head.querySelector('meta[name=csrf-token]').content,
},
success: (e, res) => {
alert('uploaded!');
},
error: (e, error) => {
console.log(error);
}
};
}
}

How to set proxy in Http request in Laravel 7?

Below is my successful HTTP request on DEV environment:
$response = Http::withHeaders([
'Content-Type' => 'application/json',
'Accept' => 'application/json'
])
->withToken('xxxxxxxxxxxxxx')
->post('https://xxxxxxxxx.com/v0.1/messages/', [
'from' => [
'type' => 'xxxx',
'number' => 'xxxxxxxx',
],
'to' => [
'type' => 'xxxxx',
'number' => 'xxxxxx',
],
'message' => [
'content' => [
'type' => 'text',
'text' => 'test message from laravel'
]
]
]);
But on production its mandatory to add a proxy to the request.
Anyone have any idea how to pass a proxy with the request above ?
Thank you in advance.
You can specify Guzzle options using the withOptions method.
Hence:
$response = Http::withOptions([
'proxy' => 'http://username:password#proxyhost.com:7000'
])->withHeaders( ...

How to pass the Zoho-oauthtoken in the Header using Guzzle on Laravel?

I have this cURL that I want to convert for Guzzle
curl_setopt_array($curl, array(
CURLOPT_URL => "https://subscriptions.zoho.com/api/v1/hostedpages/newsubscription",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS =>"{\n \"plan\": {\n \"plan_code\": \"AM-001\",\n \"price\": " . $gPrice . ",\n \"tax_id\": \"1786305000000842230\",\n },\n \"addons\": [\n {\n \"addon_code\": \"AB-001\",\n \"addon_description\": \"Ads Budget\",\n \"price\": " . $bPrice . ",\n\n }\n ],\n \"coupon_code\": \"150-credit\"\n \n}",
CURLOPT_HTTPHEADER => array(
"X-com-zoho-subscriptions-organizationid: " . $org_id,
"Authorization: Zoho-oauthtoken " . $accessToken,
"Content-Type: application/x-www-form-urlencoded"
),
));
For now, I have convert this :
$headers = [
'Authorization' => 'Zoho-oauthtoken ' . $access_token,
'X-com-zoho-subscriptions-organizationid' => $org_id,
];
$res = $client->request('POST', 'https://subscriptions.zoho.com/api/v1/hostedpages/newsubscription', $headers, [
'plan' => [
'plan_code' => 'AM-001',
'price' => $data['finaltotal'],
'tax_id' => '1786305000000842230',
],
'addons' =>[
'addon_code' => 'AB-001',
'addon_description' => 'Ads Budget',
'price' => $data['finalads']
],
'coupon_code' => '150-credit'
]);
But I have
"Client error: POST https://subscriptions.zoho.com/api/v1/hostedpages/newsubscription resulted in a 401 Unauthorized response:
{"code":14,"message":"Invalid value passed for authtoken."}"
Have I correctly defined the header?
Thank you for your help.
third option is options for request, So if you need to pass headers you need to specify key headers.
so your code should look like this
$options = [
'headers' => [ // <- here :)
'Authorization' => 'Zoho-oauthtoken ' . $access_token,
'X-com-zoho-subscriptions-organizationid' => $org_id,
];
]
$res = $client->request(
'POST',
'https://subscriptions.zoho.com/api/v1/hostedpages/newsubscription',
$options, // <- options
[
'plan' => [
'plan_code' => 'AM-001',
'price' => $data['finaltotal'],
'tax_id' => '1786305000000842230',
],
'addons' =>[
'addon_code' => 'AB-001',
'addon_description' => 'Ads Budget',
'price' => $data['finalads']
],
'coupon_code' => '150-credit'
]
);
try this it should work.
if any doubt please comment.

Net::HTTPForbidden 403 Forbidden

Httparty post response is coming as
#<Net::HTTPForbidden 403 Forbidden readbody=true>
request = HTTParty.post('http://fcm.googleapis.com/fcm/send',
:body => { "to" => "xxx:xxxx", "priority" => "high", "data" => { "title" =>"test","body"=>"cccc",'massage_type'=>'text'}}.to_json, :headers => { 'Content-Type' => 'application/json', 'Authorization' => "key=cccccc","User-Agent"=>"" } )

Jwt authentication error in laravel 5.2

I use tymon/jwt-auth package for authentication in laravel 5.2 framework
But when i run my login api i got error :
ErrorException in /var/www/vendor/composer/ClassLoader.php line 317:
Uninitialized string offset: 0
Login code :
try
{
$this->validate($request, [
'email' => 'required|email|max:255', 'password' => 'required',
]);
}
catch (HttpResponseException $e)
{
return response()->json([
'error' => [
'message' => 'Invalid auth',
'status_code' => IlluminateResponse::HTTP_BAD_REQUEST
]],
IlluminateResponse::HTTP_BAD_REQUEST,
$headers = []
);
}
try
{
if ( ! $token = JWTAuth::attempt(['email' => $request->get('email'), 'password' => $request->get('password'), 'status' => 'activate']))
{
return response()->json(['error' => 'invalid_credentials'], 401);
}
}
catch (JWTException $e)
{
return response()->json(['error' => 'could_not_create_token'], 500);
}
$user = User::where('email',$request->get('email'))->where('status','=','activate')->get(['_id','user_type']);
$this->operator->sign_in($user[0]['_id']);
$headers = [
'Access-Control-Allow-Origin' => '*',
'Access-Control-Allow-Methods'=> 'POST, GET, OPTIONS, PUT, DELETE, PATCH',
'Access-Control-Allow-Headers'=> 'Content-Type, Origin,Accept,X-Requested-With, Authorization',
'Access-Control-Expose-Headers' => 'Content-Type, Origin,Accept,X-Requested-With, Authorization',
'Access-Control-Allow-Credentials' => true,
'Authorization'=> 'Bearer' . $token
];
$this->activities->post('login');
return response()->json(['_id'=> $user[0]['_id']], 200, $headers);
And in auth.php in config folder i have :
'defaults' => [
'guard' => env('AUTH_GUARD', 'web'),
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => app('config')->get('jwt.user'),
],
// 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
],
Notice : when i change AUTH_GUARD to api in defaults section i got error :
call_user_func_array() expects parameter 1 to be a valid callback, class 'Illuminate\Auth\TokenGuard' does not have a method 'once'

Resources