title: UrlFetch put method using Google Apps Script
body: <p>Have tried many options to update a product in ECWID using Google Apps Script UrlFetchApp.fetch() put method but not succeeded. Following are the different ways that I've written the code and tested, but am getting different type of errors.</p>
  
  <p>I guess, am missing some small thing, which am not able to figure it out. Please help me to fix this issue.</p>
  
  <p>API: ECWID Products API (<a href="http://kb.ecwid.com/w/page/25285101/Product%20API#RESTAPIMethodupdateaproduct" rel="nofollow">http://kb.ecwid.com/w/page/25285101/Product%20API#RESTAPIMethodupdateaproduct</a>)
  Method: PUT (to update the product details)</p>
  
  <hr>
  
  <p>Sample Code 1:-</p>
  
  <pre><code>function updateProducts(){
  var products_authkey = "xxxxxxxx";
  
    try{
      var url ="https://app.ecwid.com/api/v1/xxxxx/product?id=xxxxxxxx&amp;secure_auth_key="+products_authkey;
      var payload = {price:62755};
  
      var options ={method:"put",ContentType:"application/json",payload:payload};    
      var result = UrlFetchApp.fetch(url, options);    
      var response = result.getContentText();
    }catch(e){
      Browser.msgBox(e);
    }
  }
  </code></pre>
  
  <p>Error:-
  "{ "error": "OTHER", "errorMessage": "Error parsing JSON: A JSONObject text must begin with '{' at character 0" }"</p>
  
  <hr>
  
  <p>Version 2:-
  Tried converting the object to json stringify, but the same error.</p>
  
  <pre><code>function updateProducts_version2(){
    try{
      var url ="https://app.ecwid.com/api/v1/xxxx/product?id=xxxxx&amp;secure_auth_key="+products_authkey;
      var payload = {price:62755};
      var payload_json = Utilities.jsonStringify(payload);
      var options ={method:"put",ContentType:"application/json",payload:payload_json,muteHttpExceptions:true};
      var result = UrlFetchApp.fetch(url, options);
      var response = result.getContentText();
      var res_code = result.getResponseCode();
      var x = 1;
    }catch(e){
      Browser.msgBox(e);
    }
  }
  </code></pre>
  
  <p>Error:-
  "{ "error": "OTHER", "errorMessage": "Error parsing JSON: A JSONObject text must begin with '{' at character 0" }"</p>
  
  <hr>
  
  <p>Version 3:- (Tried passing secure_auth_key using Authorization in headers)</p>
  
  <pre><code>function updateProducts_version3(){
    try{
      var url ="https://app.ecwid.com/api/v1/xxxxx/product?id=xxxxx";
      var payload = {price:62755};
      var headers = {Authorization: 'xxxxxxx'};
      var options = {headers:headers,method:"put",ContentType:"application/json",payload:payload};
      var options ={method:"put",ContentType:"application/json",payload:payload,muteHttpExceptions:true};
      var result = UrlFetchApp.fetch(url, options);
      var response = result.getContentText();
      var res_code = result.getResponseCode();
      var x = 1;
    }catch(e){
      Browser.msgBox(e);
    }
  }
  </code></pre>
  
  <p>Error:-
  { "error": "OTHER", "errorMessage": "API key not found in request parameters" }</p>
  
  <hr>
  
  <p>Also to note that, I've tried using DevHttpClient chrome plugin, it's updating properly.
  Which means that there's some problem the way we're using UrlFetch. Please help me in fixing this issue...</p>
  
  <p>Thanks in advance...</p>