Home > Software engineering >  Issue with POST request with API oauth2 / return: error 415
Issue with POST request with API oauth2 / return: error 415

Time:02-03

i have some issues with an API with oauth2 authentification. After i get the token, and i want to sent my POST request but it still give me a 415 error (Unsupported Media Type) i'm sure my payload's field are good because i tryed with postman and it works, but doesn't know if i have to JSON stringify the header (the payload i thinks but i'm not sure at 100%). I run my code on Google apps script so i thought the problem came frome apps script but i can get the token and send GET request on it. I put you my code below, if anyone can help this would be very helpfull, i wich you a good day, thanks in advance for you'r help. Joseph

function post_pers() {
 var url = "(my url)";
 var data = {
  "id": 32,
  "nom": "apij",
  "prenom": "joseph",
  "civiliteLongue": "Monsieur",
  "idTypePersonne": "PERSTPHYSIQUE  ",
  "ligne1Adresse": " ",
  "ligne2Adresse": " ",
  "ligne3Adresse": " ",
  "codePostal": " ",
  "commune": " ",
  "idPays": "FR",
  "iban": " ",
  "bic": " ",
  "titulaireCompte": " ",
  "domiciliationBanque": " ",
  "assujettiTva": true,
  "mediaPrefere": "Mail",
}
var payload = JSON.stringify(data);
Logger.log("payload; " payload)
Logger.log("data; " data)
 
 var header1 = {
"accept": "application/json",
"authorization": "Bearer (my access token)",
"content-type": "application/json"
}
var header = JSON.stringify(header1);
Logger.log("header; " header)
Logger.log("header1; " header1)

var options = {
    "method": "POST",
    "header": header,
    "payload": payload
}
 var response = UrlFetchApp.fetch(url, options);
 Logger.log(response)
  }

CodePudding user response:

415 is unsupported media. This is usually due to Content-Type header typos. Your script is stringifying headers, which would make header unreadable by the server. Try

var options = {
    "method": "POST",
    "header": /*header*/header1,
    "payload": payload
}
  •  Tags:  
  • Related