Skip to main content
Notice removed Authoritative reference needed by Niraj Patel
Bounty Ended with Diana's answer chosen by Niraj Patel
Tweeted twitter.com/StackMagento/status/1361646519884128256
added 118 characters in body
Source Link
Niraj Patel
  • 935
  • 3
  • 39
  • 80

I have created rest api for customers who update the one customer field. This is below code to updaye customer by REST API Any other way to update the customer by REST API without the pass mandatory fields ?

I HAVE TO UPDATE ONLY ONE FIELD taxvat for that I have to forcefully passed the firstname,lastname,email,websiteId,email and id.

Any trick in REST API to update the one field without passing all mandatory fields or just passing by email or customer id ?

Note: Mandatory fields have values so why rest api throw message that firstname,lastname and email is missing.

<?php
$url= "https://example.com/rest";
$accessToken=  'asadsadrjt5l46jl45nmnfsdfd';
$setHeaders = array('Content-Type:application/json','Authorization:Bearer '.$accessToken);
 
$url = $url."/V1/customers/27";
$apiUrl = str_replace(" ","%20",$url);
 
$ch = curl_init();
$customerData = [
    'customer' => [
        'id' => 27,
        'email'=>'[email protected]',
        "firstname"=>"Identify",
        "lastname"=>"Identify",
        "taxvat" => "1982",
        "websiteId" => 1
        ]
    ]
];
 
$data_string = json_encode($customerData);
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $setHeaders);
$token = curl_exec($ch);
 
$response = json_decode( curl_exec($ch), TRUE);
curl_close($ch);
echo "<pre>";print_r($response);exit;
?>

I have created rest api for customers who update the one customer field. This is below code to updaye customer by REST API Any other way to update the customer by REST API without the pass mandatory fields ?

I HAVE TO UPDATE ONLY ONE FIELD taxvat for that I have to forcefully passed the firstname,lastname,email,websiteId,email and id.

Any trick in REST API to update the one field without passing all mandatory fields or just passing by email or customer id ?

<?php
$url= "https://example.com/rest";
$accessToken=  'asadsadrjt5l46jl45nmnfsdfd';
$setHeaders = array('Content-Type:application/json','Authorization:Bearer '.$accessToken);
 
$url = $url."/V1/customers/27";
$apiUrl = str_replace(" ","%20",$url);
 
$ch = curl_init();
$customerData = [
    'customer' => [
        'id' => 27,
        'email'=>'[email protected]',
        "firstname"=>"Identify",
        "lastname"=>"Identify",
        "taxvat" => "1982",
        "websiteId" => 1
        ]
    ]
];
 
$data_string = json_encode($customerData);
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $setHeaders);
$token = curl_exec($ch);
 
$response = json_decode( curl_exec($ch), TRUE);
curl_close($ch);
echo "<pre>";print_r($response);exit;
?>

I have created rest api for customers who update the one customer field. This is below code to updaye customer by REST API Any other way to update the customer by REST API without the pass mandatory fields ?

I HAVE TO UPDATE ONLY ONE FIELD taxvat for that I have to forcefully passed the firstname,lastname,email,websiteId,email and id.

Any trick in REST API to update the one field without passing all mandatory fields or just passing by email or customer id ?

Note: Mandatory fields have values so why rest api throw message that firstname,lastname and email is missing.

<?php
$url= "https://example.com/rest";
$accessToken=  'asadsadrjt5l46jl45nmnfsdfd';
$setHeaders = array('Content-Type:application/json','Authorization:Bearer '.$accessToken);
 
$url = $url."/V1/customers/27";
$apiUrl = str_replace(" ","%20",$url);
 
$ch = curl_init();
$customerData = [
    'customer' => [
        'id' => 27,
        'email'=>'[email protected]',
        "firstname"=>"Identify",
        "lastname"=>"Identify",
        "taxvat" => "1982",
        "websiteId" => 1
        ]
    ]
];
 
$data_string = json_encode($customerData);
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $setHeaders);
$token = curl_exec($ch);
 
$response = json_decode( curl_exec($ch), TRUE);
curl_close($ch);
echo "<pre>";print_r($response);exit;
?>
Notice added Authoritative reference needed by Niraj Patel
Bounty Started worth 100 reputation by Niraj Patel
Source Link
Niraj Patel
  • 935
  • 3
  • 39
  • 80

Magento 2: why we should pass all required fields to update one field of customer by REST API?

I have created rest api for customers who update the one customer field. This is below code to updaye customer by REST API Any other way to update the customer by REST API without the pass mandatory fields ?

I HAVE TO UPDATE ONLY ONE FIELD taxvat for that I have to forcefully passed the firstname,lastname,email,websiteId,email and id.

Any trick in REST API to update the one field without passing all mandatory fields or just passing by email or customer id ?

<?php
$url= "https://example.com/rest";
$accessToken=  'asadsadrjt5l46jl45nmnfsdfd';
$setHeaders = array('Content-Type:application/json','Authorization:Bearer '.$accessToken);
 
$url = $url."/V1/customers/27";
$apiUrl = str_replace(" ","%20",$url);
 
$ch = curl_init();
$customerData = [
    'customer' => [
        'id' => 27,
        'email'=>'[email protected]',
        "firstname"=>"Identify",
        "lastname"=>"Identify",
        "taxvat" => "1982",
        "websiteId" => 1
        ]
    ]
];
 
$data_string = json_encode($customerData);
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $setHeaders);
$token = curl_exec($ch);
 
$response = json_decode( curl_exec($ch), TRUE);
curl_close($ch);
echo "<pre>";print_r($response);exit;
?>