I am trying to upload files (images) over API (Swagger) using php
I am sending data from template (PHP) and after validation, in controller there is request to API with all parameters.
There are my params (from template)
$orderParameter = [
'first_name' => $first_name,
'last_name' => $last_name,
'category' => 2,
'file' => $this->request->data['documents_for_upload'][0],
];
here is response from template
[
'first_name' => 'John',
'last_name' => 'Tester',
'category' => (int) 2,
'file' => [
'name' => 'Hydrangeas.jpg',
'type' => 'image/jpeg',
'tmp_name' => 'C:\Users\jwy\AppData\Local\Temp\phpAB7B.tmp',
'error' => (int) 0,
'size' => (int) 595284
]
]
If I send those data direct (using Swagger UI with browse and select file) than its works, but if I use php I am getting error 500.
In swagger UI file field is defined like Parameter Type = formData and Data type = undefined
I have try to send only file['tmp_name'] but it doesnt work and also try to convert file encode_base64 but it is the same result.
Related
So I am trying to validate the uploaded images in my laravel project, however the File::image() validaton invalidates my input even though I am uploading a file. I am using postman to do this.
Here is my validatio rules.
'product_name' => 'required|string|unique:' . Product::class,
'product_stock_amount' => 'required|integer|numeric|gte:0',
'product_price' => 'required|integer|numeric|gte:0',
'product_price_currency' => 'required|string|'. Rule::in(config('lappee.accepted_currency')),
'product_description' => 'nullable|string',
'product_images' => [
'nullable',
File::image()->max(12 * 1024)->dimensions(Rule::dimensions()->maxWidth(config('lappee.allowed_image_size.width'))->maxHeight(config('lappee.allowed_image_size.height')))
],
Here's my postman.
I am I doing someething wrong?
Your validation expects that product_images is only one file, but you are sending product_images as an array. So you have to update your validation like this:
'product_name' => 'required|string|unique:' . Product::class,
'product_stock_amount' => 'required|integer|numeric|gte:0',
'product_price' => 'required|integer|numeric|gte:0',
'product_price_currency' => 'required|string|'. Rule::in(config('lappee.accepted_currency')),
'product_description' => 'nullable|string',
'product_images.*' => [
'nullable',
File::image()->max(12 * 1024)->dimensions(Rule::dimensions()->maxWidth(config('lappee.allowed_image_size.width'))->maxHeight(config('lappee.allowed_image_size.height')))
],
I've been tasked with figuring out how the AWS PHP sdk works so we can possibly use it for hosting customer image data for our web server. I've been able to successfully test most of the functionality of creating, managing and loading data into a bucket, but when I try to view the contents I get 'access denied'.
Going into the management console, I figured out how to set the permissions so I could view the file, either with a specific host rule or by setting both the bucket and the object world-readable.
However, no matter how I try following the examples in the PHP sdk [limited] documentation, I cannot seem to set the ACL values using the php code provided by Amazon.
Their examples just list in the place of the various value and I have tried filling in the relevant data for my bucket, object and account in those and it doesn't work. I've tried doing a getObjectAcl and sending back something similar to what was received and it doesn't work. I've tried looking at examples on-line and what little I have found doesn't work.
Here's an example of the latest I have tried:
$params = [
'ACL' => 'public-read',
'AccessControlPolicy' => [
'Grants' => [
[
'Grantee' => [
'DisplayName' => 'Owner',
'ID' => $awsId,
'Type' => "CanonicalUser"
],
'Permission' => "FULL_CONTROL"
],
[
'Grantee' => [
'DisplayName' => 'All Users',
'URI' => "http://acs.amazonaws.com/groups/global/AllUsers",
'Type' => "Group"
],
'Permission' => "READ"
],
],
'Owner' => [
'ID' => $awsId
]
],
'Bucket' => "our-test-bucket",
'Key' => "800x600.jpg"
];
$result = $awsSdk->getS3Client()->putObjectAcl($params);
The resulting output:
Fatal error: Uncaught exception 'Aws\S3\Exception\S3Exception' with
message 'Error executing "PutObjectAcl" on
"https://our-test-bucket.s3.us-east-2.amazonaws.com/800x600.jpg?acl";
AWS HTTP error: Client error: PUT
https://our-test-bucket.s3.us-east-2.amazonaws.com/800x600.jpg?acl
resulted in /project/vendor/aws/aws-sdk-php/src/WrappedHttpHandler.php
on line 191
Aws\S3\Exception\S3Exception: Error executing "PutObjectAcl" on "https://our-test-bucket.s3.us-east-2.amazonaws.com/800x600.jpg?acl";
AWS HTTP error: Client error: PUT
https://our-test-bucket.s3.us-east-2.amazonaws.com/800x600.jpg?acl
resulted in a 400 Bad Request response:
MalformedACLErrorThe XML you provided was not well-f (truncated...)
MalformedACLError (client): The XML you provided was not well-formed or did not validate against our published schema -
MalformedACLErrorThe XML you provided was not well-formed or did not validate against our published
schemaB24661919936C2DADft/***********************************************= in /project/vendor/aws/aws-sdk-php/src/WrappedHttpHandler.php on line
191
I managed to drop a trace into the guzzle html messageTrait withHeader() function so I could watch the outgoing xml in the php stream. I then compared the result with examples of the xml I found in various places in a google search and through trial and error with the comparisons, I traced the problem down to the 'DisplayName' => 'All Users' in the Group Grant. Remove that line and it appears to work.
$pubAcl = [
'Owner' => [
'ID' => $awsId,
'DisplayName' => 'Owner'
],
'Grants' => [
[
'Grantee' => [
'ID' => $awsId,
'DisplayName' => 'Owner',
'Type' => "CanonicalUser"
],
'Permission' => "FULL_CONTROL"
],
[
'Grantee' => [
'URI' => "http://acs.amazonaws.com/groups/global/AllUsers",
'Type' => "Group"
],
'Permission' => "READ"
]
]
];
(The order change was the result of testing but didn't turn out to be the problem)
I am struggling with the usage of the NotificationPusher component and the possibility to send custom parameters within the payload to apple products.
I've tried the following, since I've found this annotation within the docs on github.
$message = new Message("Hello there", [
'message' => [
'sound' => 'default'
],
'custom' => [
'lat' => 123,
'lon' => 321,
'radius' => 32,
'date' => date('Y-m-d H:i:s'),
'action' => 'update'
]
]);
This syntax sadly didn't led to the expected result. The apple devices wouldn't receive these parameters.
I've also tried this, but this also failed.
$message = new Message("Hello there", [
'message' => [
'sound' => 'default',
'custom_lat' => 123,
'custom_lon' => 321,
'custom_radius' => 32,
'custom_date' => date('Y-m-d H:i:s'),
'custom_action' => 'update'
]
]);
What is the exact syntax so send custom parameters within the payload to apple devices with a push message?
I've dug trough the source code on github and have found out, that the 'custom' key of the array wasn't extracted by the ASPN Adapter.
But I've found a piece of code that extracted the complete 'message' array, so my guess was to add the 'custom' array within the 'message' part, what then also was the solution for my problem.
$message = new Message("Hello there", [
'message' => [
'sound' => 'default',
'custom' => [
'lat' => 123,
'lon' => 321,
'radius' => 32,
'date' => date('Y-m-d H:i:s'),
'action' => 'update'
]
]
]);
I faced the same need. Started debugging and found that KhorneHoly is right: You need to send the payload under custom key. The array format is a little bit different, though:
$message = new Message('This is the message', [
'custom'=> [
'payloadKey' => 'payloadContent'
]
]);
I'm using "sly/notification-pusher": "^2.3"
Hope it helps =)
I am sending a post request to the GetResponse API. Everything works fine until I add a custom field (customFieldValues) to save along with my new email contact.
$body_data =
[
'name' => $input['name'],
'email' => $input['email'],
'campaign' => [
'campaignId' => $campaign_id
],
'customFieldValues' => ['customFieldId' => 'LDe0h', 'value' => ['Save this test string.'] ]
];
When I send the request I get the following error message:
"errorDescription": "CustomFieldValue entry must be specified as array"
I have tried a few things now and not sure how to format this properly to have the API accept it.
Reference link:
http://apidocs.getresponse.com/v3/case-study/adding-contacts
I found the solution on github in an example for their php api here:
https://github.com/GetResponse/getresponse-api-php
I suppose I had to wrap an array inside an array inside of an array...geez:
'customFieldValues' => array(
array('customFieldId' => 'custom_field_id_obtained_by_API',
'value' => array(
'Y'
)),
array('customFieldId' => 'custom_field_id_obtained_by_API',
'value' => array(
'Y'
))
)
For GetResponse V3
'customFieldValues' => [
'customFieldId' => 'custom_field_id_from_API',
'name' => 'Website',
'value' => ['https://scholarshipspsy.com']
]
Please note the 'name' field is optional. The site sampled is an international scholarship platform.
I have been thinking about a good way to handle nested/complex values in POST requests to a apigility resource.
For example, an order might contain a collection of order items in a single POST requested that is used to create an order. Both, order and order-item do exist as a resource. However, I would very much like to have only one request that would create order and order item entities. Handling that in the resource is not a problem, but I wonder how you would configure that resource (let´s call it order-place) using the apigiliy UI - or, if at all impossible, using the configuration. Applying validators and filters is one of the key features of apigility, and i´d like to keep using that, even for complex request data.
And before you ask, using an underscore to separate the values scopes, for example order_comment and order_item_comment should not be an option.
Any ideas?:)
Addition: A sample json request payload could look like this:
{
"created_at": "2000-01-01",
"amount" : "5000.00",
"address" : {
"name": "some name",
"street": "some street"
...
},
"items" : [
{"productId":99,"qty":1}
...
]
}
Starting from Wilt's answer, I've found that the following code works as well:
# file path: /module/MyApi/config/module.config.php
// some other stuff
'MyApi\\V1\\Rest\\MyRestService\\Validator' => array(
'address' => array(
0 => array(
'name' => 'name',
'required' => true,
'filters' => array(),
'validators' => array(),
),
1 => array(
'name' => 'street',
'required' => true,
'filters' => array(),
'validators' => array(),
),
'type' => 'Zend\InputFilter\InputFilter'
),
'amount' => array(
'name' => 'amount',
'required' => true,
'filters' => array(),
'validators' => array()
)
The only problem I get is when address is passed as a field (string or numeric) rather then an array or object. In this case Apigility throws an exception:
Zend\InputFilter\Exception\InvalidArgumentException: Zend\InputFilter\BaseInputFilter::setData expects an array or Traversable argument; received string in /var/www/api/vendor/zendframework/zendframework/library/Zend/InputFilter/BaseInputFilter.php on line 175
Adding address as a further simple (required) field avoids the exception, but then Apigility doesn't see any difference whether we pass address as an array of name and street or a dummy string.
If you are using the ContentValidation module then you can configure an input filter for the nested resources by assigning it to a variable. Then you have to add a type key (essential otherwise reusing the filter won't work). Now you are able to use this variable in your input_filter_specs and you can reuse the whole filter inside another filter. So something like this in your config.php:
<?php
namespace Application;
// Your address config as if it was used independently
$addressInputFilter => array(
'name' => array(
'name' => 'name',
'required' => true,
'filters' => array(
//...
)
'validators' => array(
//...
)
),
'street' => array(
'name' => 'street',
'required' => true,
'filters' => array(
//...
)
'validators' => array(
//...
)
),
// 'type' key necessary for reusing this input filter in other configs
'type' => 'Zend\InputFilter\InputFilter'
),
'input_filter_specs' => array(
// The key for your address if you also want to be able to use it independently
'Application\InputFilter\Address'=> $addressInputFilter,
// The key and config for your other resource containing a nested address
'Application\InputFilter\ItemContainingAddress'=> array(
'address' => $addressInputFilter,
'amount' => array(
'name' => 'amount',
'required' => true,
'filters' => array(
//...
),
'validators' => array(
//...
)
)
//... your other fields
)
)