Comments (11)
Dear mevdschee
Thanks for your quick response.
Yes, I wrote in Swift 5.
My code using "PUT" and set json as Contents-Type, and set httpBody with params
can modify my server data which I can checked PhpAdmin.
request.httpMethod = "PUT"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpBody = try JSONSerialization.data(withJSONObject: params, options: [])
I do not use any proxy. I set up the local in M1 Mac and set up the mkcert for https connection.
I check the php_error.log
when I try PUT,
PHP Notice: Trying to access array offset on value of type int in /Applications/MAMP/htdocs/api.php on line 12762
and success modification.
When I try POST
PHP Notice:Undefined index: records in /Applications/MAMP/htdocs/api.php on line 12762
line 12762 is
return ResponseFactory::fromObject(200, $json->records,0);
So It seems that something "customization.afterHandler' issue for POST?. But If this customization.afterHandler is missing , I cannot display native JSON format.
Actually POST is to add new record, so no index should be.
Any suggestion?
from php-crud-api.
Dear mevdschee
Thanks for your quick response.
My understanding is following afterhandler is to display JSON form when received GET from client.
If this afterhandler is used as POST, how do I modify this afterhandler?
I am new be of php. So some sample code you can provide?
'middlewares' => 'cors,customization',
'customization.afterHandler' => function ($operation, $tableName, $response, $environment) {
$json = json_decode($response->getBody()->getContents());
return ResponseFactory::fromObject(200, $json->records,0);
}
from php-crud-api.
Dear mevdschee
I tried with small table with accept null condition. and can POST successfully even if I stay with 'customization.afterHandler'.
My initial failure will be happened with large table without null accept, and only few parts of param will be POSTED.
Thanks for your very quickly and helpful advice.
from php-crud-api.
Dear mevdschee
I explain as much as clear.
Initial test condition was
- Database records have more than 3 records and each recode have no NULL acceptable condition. for example (4 records, actually my records had 15 records)
"id" <- Primary Key
"sku"
"Code"
"Name" - In this condition, I made PUT, DELETE, GET successfully.
- So I tried to test POST with following code. This data is just 3 records.
// set the method(HTTP-POST)
request.httpMethod = "POST"
// set the header(s)
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
// set the request-body(JSON)
let params: [String: Any] = [
"id": "3",
"sku":"JP",
"Code":"DDD"
]
do{
request.httpBody = try JSONSerialization.data(withJSONObject: params, options: [])
}catch{
print(error.localizedDescription)
}
- In this case, result shows
{"code":1010,"message":"Data integrity violation"} - Then I changed the Database condition with NULL acceptable for each records except "id"
- then same 3 records POST will successfully completed.
- then I changed back again with NON NULL acceptable condition, then POST all records made successfully completed.
No change api.php which has following customization.
'middlewares' => 'cors,customization',
'customization.afterHandler' => function ($operation, $tableName, $response, $environment) {
$json = json_decode($response->getBody()->getContents());
return ResponseFactory::fromObject(200, $json->records,0);
}
I do not sure my approach is reasonable or not, but I can POST as of now.
from php-crud-api.
Dear mevdschee
I understood.
Thanks for your great support.
I need to continue studying these area.
from php-crud-api.
Thank you for asking this question. Your client is written in Swift, am I right? I seem to recognize some keywords from here: https://developer.apple.com/library/archive/documentation/NetworkingInternetWeb/Conceptual/NetworkingOverview/WorkingWithHTTPAndHTTPSRequests/WorkingWithHTTPAndHTTPSRequests.html
I'm looking forward to help you. Do you use mitmproxy or an equivalent proxy such as charlesproxy?
from php-crud-api.
Any suggestion?
Sure, in the "afterHandler" you assume the response has a property "records", but a POST does not have that, that's why it might be failing.
'customization.afterHandler' => function ($operation, $tableName, $response, $environment) {
$json = json_decode($response->getBody()->getContents());
return ResponseFactory::fromObject(200, $json->records,0);
}
I hope this helps.
from php-crud-api.
It sounds like you copied something without understanding what it is doing. You can leave out the 'customization.afterHandler' and everything will work just fine. Some people argue that responses (on 'list' operations) should not contain the "records" key and they use the 'customization.afterHandler' to get rid of that.
I hope this helps.
from php-crud-api.
My initial failure will be happened with large table without null accept, and only few parts of param will be POSTED.
I don't know what "table without null accept" means, can you explain this a little bit further please? Also you write "only few parts of param will be POSTED" and what parts is that, can you elaborate? Can you give an example of the request, the expected result and the actual result? I want to help, but I do need a bit more information to go by.
Kind regards, Maurits
from php-crud-api.
It seems you don't POST "Name" while that field is not nullable and does not have a default. That may be the cause of the "Data integrity violation".
from php-crud-api.
Let me know if you have further questions. Feel free to open a new issue.
from php-crud-api.
Related Issues (20)
- Create record when there is a many to many relation HOT 2
- Sort joined table HOT 1
- Add filter for join HOT 7
- Uncaught Error: Class "Tqdev\PhpCrudApi\Config" not found in api.php:12741 HOT 2
- Opening up or Disabling CORS HOT 1
- Filter results if joined table has an authorization.recordHandler HOT 3
- HTML Middleware HOT 2
- Update with "where" HOT 8
- "Request Tranformer" middleware HOT 2
- Azure App Service HOT 1
- Add PHP backend to any app project with one command HOT 1
- Any advice on events integration? HOT 2
- Synchronization between local IndexedDB and MySQL Database. HOT 3
- Can the API Support Customized Cache Management for Specific Tables? HOT 9
- Question: Add property on create HOT 3
- Insights on caching with Redis HOT 2
- Using Customization handler to insert app-generated ID HOT 11
- PUT using curl comes back successful but the record is not actually updated HOT 2
- Suggestion to modify response JSON root Key (records) HOT 2
- firebase jwt public key rotation HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from php-crud-api.