4

I am trying to send a POST request using Google Apps Script. According to the documentation, the request consists of three parts:

  1. Url
  2. Header
  3. Data

But UrlFetchApp.fetch only allows 2 parameters.

I tried to implement it in completely different ways.

At the moment, my code looks like this:

function qwe(){
  
const url = 'https://api-seller.ozon.ru/v1/product/info/prices';
const data = {
  "page": 1,
  "page_size": 100};

const response = UrlFetchApp.fetch(url, { 
    body: JSON.stringify(data), // данные могут быть 'строкой' или {объектом}!
    headers: {
      "Host": "api-seller.ozon.ru", 
      "Client-Id": "28000",
      "Api-Key": "65db5b96-cbb6-4b68-8f33-c8c000000000000",
      "Content-Type": "application/json"
    }
  });
  const json = response.json();
  console.log('Done:', JSON.stringify(json));
}

I get the error "Exception: request failed for https://api-seller.ozon.ru returned code 405" Please, tell me how this can be done?


Thank you Tanaike!!

Working version of the code:

function qwe(){
  
  const url = 'https://api-seller.ozon.ru/v1/product/info/prices';
  const formData = {
  "page": 1,
  "page_size": 100};

  const headers = { 
      "Client-Id": "28100",
      "Api-Key": "65db5b96-cbb6-4b68-8f33-c8c000000000"
    };
  Logger.log(JSON.stringify(formData));

  const options = { 
    'method' : 'post',
    'contentType': 'application/json',
    'headers': headers,
    'payload': JSON.stringify(formData)
  };

  const response = UrlFetchApp.fetch(url, options);
  //Logger.log(response);
  var data = JSON.parse(response);
  Logger.log(data);
  //const json = response.json();
  //console.log('Успех:', JSON.stringify(json));
}
Elazzi
  • 41
  • 1
  • 4
  • In your script, when `body` is modified to `payload`, what result will you obtain? [Ref](https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app) If an error occurs, can you provide the document of the specification of API you want to use? – Tanaike Feb 11 '21 at 12:56
  • https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app#fetch(String,Object) – Cooper Feb 11 '21 at 12:59
  • 2
    **Thank you Tanaike!** Thanks to your comment, I was able to figure it out. Attached a working version of the code in a post. – Elazzi Feb 11 '21 at 14:21
  • Thank you for replying. I'm glad your issue was resolved. In that case, can you post it as an answer? By this, it will be useful for other users who have the same issue. – Tanaike Feb 11 '21 at 23:28
  • Tanaike, to post an answer so I can accept it) – Elazzi Feb 15 '21 at 09:26

1 Answers1

-1

check one more time if You are using https scheme.

const baseUrl = 'https://api-seller.ozon.ru/';
cane
  • 892
  • 1
  • 10
  • 16