How do i send a post request to my Slack Weeb Hook - laravel

I have this code here. Basically i want to make a post request to my weebhook but it is not making a successful request and it is returning 400 as a status code , what i am missing
Route::get('/testime',function (){
$response = Http::post(env('SLACK_WEBHOOK'),[
'content' => "Testing testing",
'embeds' => [
[
'title' => "Test ",
'description' => "URRA",
'color' => '7506394',
]
],
]);
return $response->status();
});

You are receiving a 400 error, which is Slack telling you, your data is missing or malformed. To debug you can do the following. Which will most likely state what is wrong.
dd($response->body);
Your structure seems like nothing Slack supports, which most likely will be the error message. See examples here, not certain this will even work.
$response = Http::post(env('SLACK_WEBHOOK'), [
'channel' => 'Your channel id',
'text' => 'Testing testing',
'blocks' => [
[
'type' => 'header',
'text' => [
'type' => 'plain_text',
'text' => 'URRA',
'color' => '#36a64f',
],
],
],
]);

Related

error trying to create order in paypal with guzzle and laravel

I am trying to create an order in paypal with laravel and guzzle and it throws me this error:
GuzzleHttp\Exception\ClientException Client error: POST https://api-m.sandbox.paypal.com/v2/checkout/orders resulted in a
400 Bad Request response:
{"name":"INVALID_REQUEST","message":"Request is not well-formed,
syntactically incorrect, or violates schema.","debug_id (truncated...)
my controller code:
$accessToken = $this->getAccessToken(); $client = new Client(['base_uri' => 'https://api-m.sandbox.paypal.com/v2/checkout/']);
$headers = [
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . $accessToken,
];
$params = [
'intent' => 'CAPTURE',
'purchase_units' => [
'amount' => [
'currency_code' => 'USD',
'value' => '100.00'
]
]
];
//dd($params);
$response = $client->request('POST', 'orders', [
'headers' => $headers,
'form_params' => $params
]);
Your $params object is invalid. It should be a list of purchase_unit items, not an associative array.
$params = [
'intent' => 'CAPTURE',
'purchase_units' => [
[
'amount' => [
'currency_code' => 'USD',
'value' => '100.00'
]
]
]
];
https://developer.paypal.com/api/orders/v2/#orders-create-request-body
'form_params' => $params
This is used to send an application/x-www-form-urlencoded POST request, which the PayPal API does not use.
You should be posting a plain string, JSON encoded. In place of form_params try passing the json key to guzzle in the request, or read its documentation on how to send json
edit: not sure whether that change makes a difference, but the other answer is correct that purchase_units needs to be an indexed array of purchase_unit objects -- likely only one of them.

Laravel: Add custom data to resource

First I get the translator by his id using this line of code
$translator = Translator::where('id', $translator_id)->first();
Then I send a notification to him by this code:
$response = Http::withHeaders([
'Authorization' => 'key=myKey',
'Content-Type' => 'application/json'
])->post('https://fcm.googleapis.com/fcm/send', [
"notification" => [
"title" => "title",
"body" => "body",
],
"data" => [
"title" => "title",
"body" => "body",
],
"to" => $token,
]);
Everything works fine but my problem is that when I return the TranslatorResource I want to add the notification response to it, so I do this in my controller
$resource = new TranslatorResource($translator);
$resource->notif = $response;
return $resource;
And in TranslatorResource I have this code:
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'phone' => $this->phone,
'cv' => $this->cv,
'specialization' => $this->specialization,
'tr_languages' => $this->tr_languages,
'all_languages' => $this->all_languages,
'isVerified' => $this->isVerified == 0 ? false : true,
'isActive' => $this->isActive == 0 ? false : true,
'completed_orders' => $this->completed_orders,
'canceled_orders' => $this->canceled_orders,
'rejected_orders' => $this->rejected_orders,
'current_orders' => $this->current_orders,
'isTranslator' => true,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
];
}
But I only get the data specified in the resource, the notif key isn't added, anyone know how to add this data to my resource when I return it ?
You can use additional method provided by laravel.
return (new TranslatorResource($translator))->additional(['notif ' => $response]);
Reference: Eloquent: API Resources
You can look for the section Adding Meta Data When Constructing Resources.

CakePHP IntegrationTestTrait Post Returns HTTP Status 302

I am trying to perform a simple test from posting data onto a table in CakePHP. I am using IntegrationTestTrait.
I am not able to POST data successfully. My $this->_response is yielding:
object(Cake\Http\Response) {
'status' => (int) 302,
'contentType' => 'text/html',
'headers' => [
'Content-Type' => [
(int) 0 => 'text/html; charset=UTF-8'
],
'Location' => [
(int) 0 => '/'
]
],
'file' => null,
'fileRange' => [],
'cookies' => object(Cake\Http\Cookie\CookieCollection) {
[protected] cookies => []
},
'cacheDirectives' => [],
'body' => ''
}
My TestCase code looks like this:
public function testAddStudentSuccess() {
$data = [
'last_name' => 'Test',
'first_name' => '05',
'middle_name' => '',
'preferred_name' => '',
'id_number' => '10005',
'contact_id' => '',
'users[0][email]' => 'test_05#email.com'
];
//Test Pre-condition
$query = $this->Students->find('all')->where([
'id_number' => $data['id_number']
]);
$this->post('/students/add', $data);
debug($this->_response);
}
I debugged further and found that the Test is not even invoking the Controller add() functions.
I thought the issue was an Authentication Issue is I tried following all the authentication work arounds prescribed in the documentation. However, it did not work.
Does anyone know how I can debug this further? Any help is appreciated. Thank you.

Validate all possibilities of two outcomes

So I'm getting a callback with two outcomes:
positive: url.com?scope=read_write&code={AUTHORIZATION_CODE}&state={csrf}
negative: url.com?error=access_denied&error_description=The%20user%20denied%20your%20request
I would like to validate all possibilities. My current approach is:
$data = request()->validate([
'scope' => 'required_without:error, error_description',
'code' => 'required_without:error, error_description',
'state' => 'required_without:error, error_description',
'error' => 'required_without:scope,code,state',
'error_description' => 'required_without:scope,code,state'
]);
Actually, it works super fine for every possibility but somehow not for:
$this->get(route('connect.index', [
'error' => $this->error,
'error_description' => $this->error_description,
]));
dd(session('errors'));
The weird part is that I get the error:
#messages: array:3 [
"scope" => array:1 [
0 => "The scope field is required when error / error description is not present."
]
"code" => array:1 [
0 => "The code field is required when error / error description is not present."
]
"state" => array:1 [
0 => "The state field is required when error / error description is not present."
]
]
But that is not true since I see that the values error, error_description are present!
array(2) {
["error"]=>
string(2) "ok"
["error_description"]=>
string(5) "state"
}
Update
#DigitalDrifter helped me, but now, I have got two new test-cases that fail!
$this->get(route('connect.index', [
'scope' => $this->scope,
'code' => $this->code,
'error' => $this->error,
'error_description' => $this->error_description,
]))
->assertStatus(302);
$this->get(route('connect.index', [
'scope' => $this->scope,
'code' => $this->code,
'state' => $this->error,
'error_description' => $this->error_description,
]))
->assertStatus(302);
As you might see I'm expecting to get a Response of 302 but I get 200. It should not be the case. I'm using #DigitalDrifter answer:
$data = request()->validate([
'scope' => 'bail|sometimes|required_without:error,error_description',
'code' => 'bail|sometimes|required_without:error,error_description',
'state' => 'bail|sometimes|required_without:error,error_description',
'error' => 'bail|required_without:scope,code,state',
'error_description' => 'bail|required_without:scope,code,state',
]);
Might have luck with sometimes:
Validating When Present
In some situations, you may wish to run validation checks against a field only if that field is present in the input array. To quickly accomplish this, add the sometimes rule to your rule list:
$data = request()->validate([
'scope' => 'bail|sometimes|required_without:error,error_description',
'code' => 'bail|sometimes|required_without:error,error_description',
'state' => 'bail|sometimes|required_without:error,error_description',
'error' => 'bail|required_without:scope,code,state',
'error_description' => 'bail|required_without:scope,code,state'
]);

Post Multipart and Json together with Guzzle in Laravel

I'm trying to POST multipart and json data with Guzzle to build my apps with Phonegap Build API. I've tried many adjustment but still got error results. Here's the latest function I'm using:
public function testBuild(Request $request)
{
$zip_path = storage_path('zip/testing.zip');
$upload = $this->client->request('POST', 'apps',
['json' =>
['data' => array(
'title' => $request->title,
'create_method' => 'file',
'share' => 'true',
'private' => 'false',
)],
'multipart' =>
['name' => 'file',
'contents' => fopen($zip_path, 'r')
]
]);
$result = $upload->getBody();
return $result;
}
This is my the correct curl format that has success result from the API, but with file I have in my desktop:
curl -F file=#/Users/dedenbangkit/Desktop/testing.zip
-u email#email.com
-F 'data={"title":"API V1 App","version":"0.1.0","create_method":"file"}'
https://build.phonegap.com/api/v1/apps
As mentioned before, you cannot use multipart and json together.
In your curl example it's just a multipart form, so use the same in Guzzle:
$this->client->request('POST', 'apps', [
'multipart' => [
[
'name' => 'file',
'contents' => fopen($zip_path, 'r'),
],
[
'name' => 'data',
'contents' => json_encode(
[
'title' => $request->title,
'create_method' => 'file',
'share' => 'true',
'private' => 'false',
]
),
]
]
]);

Resources