Coder Social home page Coder Social logo

Comments (11)

junmcenroe avatar junmcenroe commented on June 18, 2024 1

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.

junmcenroe avatar junmcenroe commented on June 18, 2024 1

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.

junmcenroe avatar junmcenroe commented on June 18, 2024 1

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.

junmcenroe avatar junmcenroe commented on June 18, 2024 1

Dear mevdschee

I explain as much as clear.
Initial test condition was

  1. 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"
  2. In this condition, I made PUT, DELETE, GET successfully.
  3. 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)
}


  1. In this case, result shows
    {"code":1010,"message":"Data integrity violation"}
  2. Then I changed the Database condition with NULL acceptable for each records except "id"
  3. then same 3 records POST will successfully completed.
  4. 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.

junmcenroe avatar junmcenroe commented on June 18, 2024 1

Dear mevdschee

I understood.
Thanks for your great support.
I need to continue studying these area.

from php-crud-api.

mevdschee avatar mevdschee commented on June 18, 2024

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.

mevdschee avatar mevdschee commented on June 18, 2024

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.

mevdschee avatar mevdschee commented on June 18, 2024

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.

mevdschee avatar mevdschee commented on June 18, 2024

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.

mevdschee avatar mevdschee commented on June 18, 2024

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.

mevdschee avatar mevdschee commented on June 18, 2024

Let me know if you have further questions. Feel free to open a new issue.

from php-crud-api.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.