I am trying to send a POST request using Google Apps Script. According to the documentation, the request consists of three parts:
- Url
- Header
- 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)); }
bodyis modified topayload, what result will you obtain? Ref If an error occurs, can you provide the document of the specification of API you want to use?