简体   繁体   中英

Send a POST request using Google Apps Script

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));
}

check one more time if You are using https scheme.

const baseUrl = 'https://api-seller.ozon.ru/';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM