• Resolved mcdouchedy

    (@mcdouchedy)


    All is working well when using the API except when trying to create or change a record in the DB: the fields in the body of my request are not updated. It’s to say, the custom fields I created; some fields like ‘last modified’ are updated.

    Any clue? I tried a lot, but without success. ( even the delete of a record works, so I guess I have enough privileges to do updates or create new records )

    Postman: simplest call (POST or PATCH) to change a field ‘make’ of record 4 (which works very well in the admin console)

    PATCH /wp-json/participants-database/v1/record/update/4

    HTTP/1.1
    Host: https://www.paragarage.be
    Content-Type: application/json
    Authorization: Basic TWNEb3Vja***************************************GRVN08gMFVCNg==
    Cookie: PHPSESSID=rpcc32b9q08g0pe44nbn2ih43k
    Content-Length: 23

    {
    “make”: “Ozone”
    }

    Log: Particpants DB ( always returns the unmodified value, the ‘data_updated’ changed )

    [08/07/24 2:19pm UTC] PDb_submission\main_query\base_query::execute_query storing record: UPDATE wp_participants_database SET date_updated = “2024-08-07 14:19:34”, make = ‘Niviu’, approved = ‘yes’, last_update_user = ‘McDouchedy’ WHERE id = 4 [08/07/24 2:19pm UTC]

    PDb_Participant_Cache::reload_cache: Refreshing Participants Database cache for cache group 0

    [08/07/24 2:20pm UTC] PDb_List_Admin::initialize list query: SELECT * FROM wp_participants_database p ORDER BY p.date_updated desc LIMIT 0,200

    THANKS FOR THE HELP!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author xnau webdesign

    (@xnau)

    First, the API will only respond to POST or GET requests.

    You don’t mention what the response code was, it should be a 200 if successful. If a POST request is not successful, you probably are not authorizing the request properly. This is explained in the documentation.

    I just tested this operation and it is working.

    Thread Starter mcdouchedy

    (@mcdouchedy)

    OK, I stick to POST and GET from now on. The response was/is still 200 for a POST ( to add or update a record ). When I authorize the request for a delete, it’s working: the empty record created with my previous add is deleted.

    Here’s the output of another tool:

    POST:

    {

        "uri": "https://www.paragarage.be/wp-json/participants-database/v1/record/add",

        "method": "POST",

        "headers": {

            "accept": "*/*",

            "Accept-Encoding": "gzip, deflate, br",

            "Connection": "keep-alive",

            "Content-Type": "application/json"

        },

        "authentication": {

            "username": "McDouchedy",

            "password": "*sanitized*",

            "type": "Basic"

        },

        "body": {

            "make": "Ozone",

            "model": "Niviuk",

            "serial": "1234"

        }

    }

    RESPONSE:

    {
    "statusCode": 200,
    "headers": {
    "Server": "nginx",
    "Date": "Sat, 10 Aug 2024 05:53:09 GMT",
    "Transfer-Encoding": "chunked",
    "Connection": "keep-alive",
    "Pragma": "no-cache",
    "X-Robots-Tag": "noindex",
    "Link": "<https://www.paragarage.be/wp-json/>; rel=\"https://api.w.org/\"",
    "X-Content-Type-Options": "nosniff",
    "Access-Control-Expose-Headers": "X-WP-Total, X-WP-TotalPages, Link",
    "Access-Control-Allow-Headers": "Authorization, X-WP-Nonce, Content-Disposition, Content-MD5, Content-Type",
    "Cache-Control": "no-store, must-revalidate, no-cache, max-age=0, private",
    "Set-Cookie": "PHPSESSID=q6dam9rn4jj4lk1iv9p9icss2u; path=/",
    "Content-Type": "application/json; charset=utf-8",
    "Allow": "POST,PUT,PATCH",
    "Expires": "Wed, 11 Jan 1984 05:00:00 GMT",
    "Content-Length": "15"
    },
    "body": {
    "record_id": 7
    }
    }

    EMPTY record created with id 7 ( not sure if you see the image ):

    make model serial report validity Administrative Info Approved Record Info Last Updater Record ID read only Private ID Date Recorded

    Delete the record:

    {

        "uri": "https://www.paragarage.be/wp-json/participants-database/v1/record/delete/7",

        "method": "POST",

        "headers": {

            "accept": "*/*",

            "Accept-Encoding": "gzip, deflate, br",

            "Connection": "keep-alive",

            "Content-Type": "application/json"

        },

        "authentication": {

            "username": "McDouchedy",

            "password": "*sanitized*",

            "type": "Basic"

        }

    }

    Answer:

    {

        "statusCode": 200,

        "headers": {

            "Server": "nginx",

            "Date": "Sat, 10 Aug 2024 05:58:35 GMT",

            "Transfer-Encoding": "chunked",

            "Connection": "keep-alive",

            "Pragma": "no-cache",

            "X-Robots-Tag": "noindex",

            "Link": "<https://www.paragarage.be/wp-json/>; rel=\"https://api.w.org/\"",

            "X-Content-Type-Options": "nosniff",

            "Access-Control-Expose-Headers": "X-WP-Total, X-WP-TotalPages, Link",

            "Access-Control-Allow-Headers": "Authorization, X-WP-Nonce, Content-Disposition, Content-MD5, Content-Type",

            "Cache-Control": "no-store, must-revalidate, no-cache, max-age=0, private",

            "Set-Cookie": "PHPSESSID=lvjj8o55un3s46rimkese84o30; path=/",

            "Content-Type": "application/json; charset=utf-8",

            "Allow": "POST,PUT,PATCH",

            "Expires": "Wed, 11 Jan 1984 05:00:00 GMT",

            "Content-Length": "4"

        },

        "body": true

    }

    The result is that the record is deleted.

    That’s why I think the authentication is ok. I only have one user on this setup and it’s also the admin. The possibility to make anonymous request is not enabled.

    I have the impression that the fieldnames in my request doesn’t match the fields in the record, and that’s why the record is created and all the fields are empty but I took the output of a GET record and pasted the body into a POST. Is there something with the encoding that I should check? I already emptied the DB and started over again with a new one. I don’t know anymore what I should check or test… Any hints are welcome ??

    • This reply was modified 7 months, 2 weeks ago by mcdouchedy. Reason: Image not visible
    Plugin Author xnau webdesign

    (@xnau)

    Yes, the authentication is OK.

    Looking at the header, it looks like the content type should be set to “multipart/form-data”, you’re using “application/javascript” I don’t know if that is the problem, but that’s the only difference from what is working in my tests here.

    Thread Starter mcdouchedy

    (@mcdouchedy)

    Thanks a lot Mr Barker, this was indeed the problem. The syntax of the body is a bit more complex than with JSON, but it does the job and that’s the most important.

    {

    ? ? "uri": "https://www.paragarage.be/wp-json/participants-database/v1/record/update/11",

    ? ? "method": "POST",

    ? ? "headers": {

    ? ? ? ? "content-type": "multipart/form-data; boundary=separator",

    ? ? ? ? "accept": "*/*",

    ? ? ? ? "accept-encoding": "gzip, deflate, br",

    ? ? ? ? "connection": "keep-alive"

    ? ? },

    ? ? "authentication": {

    ? ? ? ? "username": "McDouchedy",

    ? ? ? ? "password": "*sanitized*",

    ? ? ? ? "type": "Basic"

    ? ? },

    ? ? "body": "--separator\nContent-Disposition: form-data; name=\"make\"\n\nrebelie\n--separator--"

    }
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘API POST/PATCH doesn’t update (or create) the record’ is closed to new replies.