0

I was trying to update some customer records on an API when this question struck.

First I request the customer object from the API and transform it into an array, the update I have to perform consists on appending a quite big second array to the one I first got, so I decided to do so and then, use UPDATE to upload the whole new array as substitute for the old one.

Since this is the actual API and every change I made would go directly into production, I am not sure if this is going to work, and I thought that I could always delete the old object and make a simple POST request to upload the new one.

This is the method tasked to upload the new array ($response).

function updateReceipt($response, $receiptId, $uri4, $token){
    $ch = curl_init();

    $options = array(
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_URL => $uri4.$receiptId,
        CURLOPT_CUSTOMREQUEST => 'PUT',
        CURLOPT_POSTFIELDS => http_build_query($response),
        CURLOPT_HTTPHEADER => array("Content-Type: application/json", "key: ".$token)
    );
    curl_setopt_array($ch, $options);

    $response = curl_exec($ch);
    $response = json_decode($response, true);

    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    echo "Status code: $http_code ";

    curl_close($ch);

    return $updated;
}

So, will the PUT method accept the whole array and update the customer record? Does PUT require more accurate changes to be done for it to wrok?

What do you think about the DELETE first, POST next solution? Is it going to be more efficient?

Thank you all for your help!

Berny
  • 121
  • 14
  • 2
    This really depends on how the API is developed - and also on what else DELETE will do. Have you considered the possibility that DELETE will not only delete your customer, but also all relationship, including addresses, orders, financial information, etc. When you then create a new one, none of the other relationships will be restored. – Aleks G Feb 18 '20 at 13:20
  • 1
    Are both the API itself and the access/requests to it written/controlled by you? Or are you only in control of one half of the equation? – Patrick Q Feb 18 '20 at 13:21
  • @AleksG answering PatrickQ's question also: I am not in control of the API, so yeah, I can't see how it works inside or the relations made. I think i will stick with PUT then, thank you guys I did not consider that issue at first! :) – Berny Feb 18 '20 at 13:26
  • Does this answer your question? [How to make HTTP POST web request](https://stackoverflow.com/questions/4015324/how-to-make-http-post-web-request) – Henry Henrinson Feb 18 '20 at 23:29

1 Answers1

2

If you are sure about that the old object is not referenced to other data , then you can do delete_post, but if it is related to some other data then it is not the right solution.

Saher Siddiqui
  • 363
  • 3
  • 15