5

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)); } 
5
  • In your script, when body is modified to payload, what result will you obtain? Ref If an error occurs, can you provide the document of the specification of API you want to use? Commented Feb 11, 2021 at 12:56
  • developers.google.com/apps-script/reference/url-fetch/… Commented Feb 11, 2021 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. Commented Feb 11, 2021 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. Commented Feb 11, 2021 at 23:28
  • Tanaike, to post an answer so I can accept it) Commented Feb 15, 2021 at 9:26

1 Answer 1

-1

check one more time if You are using https scheme.

const baseUrl = 'https://api-seller.ozon.ru/'; 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.