0

I need to do a PUT method.

The API manual brings example codes only in CURL or PHP. I need help to translate this to app script.

Here is the code from the manual:

enter image description here

Here are my variables:

idLoja = 203700218

codigo = HSSK81CASACORKITRS

apikey = 7a45522de52056f24f56386833fcdc2f3a905a92df9080728e1570ac1a7683510fb43398

My code is as follows, but it is not working. It returns that the API key parameter was not found. I need help!

function putProduct() {

var url = 'https://bling.com.br/Api/v2/produtoLoja/203700218/HSSK81CASACORKITRS/json/?apikey=7a45522de52056f24f56386833fcdc2f3a905a92df9080728e1570ac1a7683510fb43398'

     var payload = {xml: '<?xml version="1.0" encoding="UTF-8"?> 
   <produtosLoja> 
   <produtoLoja<idLojaVirtual>203700218</idLojaVirtual> 
   <preco> 
  <preco>415,98</preco<precoPromocional>389,99</precoPromocional> 
  </preco><idFornecedor>0</idFornecedor><idMarca>2</idMarca> 
  <categoriasLoja><categoriaLoja> 
  <idCategoria>3709181</idCategoria> 
  </categoriaLoja></categoriasLoja></produtoLoja></produtosLoja>'
   };
const options =
 {
 method: 'PUT',
 followRedirects: true,
 muteHttpExceptions: true,
 payload: payload,
 };

var response = UrlFetchApp.fetch(url,options);
Logger.log(response.getContentText()); 

}

  • According to [this](https://stackoverflow.com/questions/69030165/put-method-appscript-api-key-not-found) urlFetchApp has a put method – Cooper Sep 02 '21 at 13:00
  • Does this help? https://stackoverflow.com/a/68888957/11551468 – Rafa Guillermo Sep 02 '21 at 13:32
  • The code you show us in your question cannot be the code you are using, because it contains syntax errors. For example, your XML payload `var newdata = { ... }` has a couple of different errors: (1) The XML string needs to be surrounded with backticks, because you have a multi-line string. (2) The object inside the braces `{` and `}` needs to contain a key as well as a value: `{ xml: ... }`. – andrewJames Sep 02 '21 at 13:59
  • Take a look at the example provided in the [official documentation](https://ajuda.bling.com.br/hc/pt-br/articles/4403753057047-API-do-Bling-via-Google-Script). Although, please note that the official example is also wrong - it is missing the backticks needed for the multi-line string. – andrewJames Sep 02 '21 at 13:59
  • @andrewjames I have corrected the xml.. However, it returns as the apikey was not found. – user3284182 Sep 02 '21 at 15:32
  • You should edit your question to show the corrections. We need to see the code you are actually using. – andrewJames Sep 02 '21 at 15:34
  • Just did it @andrewjames – user3284182 Sep 02 '21 at 15:36
  • OK - thank you for the update. That still looks wrong to me. Your code still gives me a syntax error when I paste it into the Apps Script editor. Your multi-line string needs to [use backticks](https://stackoverflow.com/questions/805107/creating-multiline-strings-in-javascript). Also, I am not suggestion this will resolve your underlying problem - but it is harder to help when we know we have code which cannot run. – andrewJames Sep 02 '21 at 15:41
  • @user3284182 Can you create new keys and try again? Do you get a different result? – Aerials Sep 16 '21 at 09:46

0 Answers0