Python Forum

Full Version: POST `api` 415 (Unsupported Media Type)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
A little off the scope of Python but I'm trying to get things done in the frontend before integrating the Django backend.
I have tried different header objects to this simple html page from where I'm to send Post request to an API endpoint. It's a form data of username and the password. I keep getting
Error:
POST https://some/api/endpoint/ 415 (Unsupported Media Type)
error message.


<!DOCTYPE html>
<html lang="en">
  <head>
    <link rel="stylesheet" type="text/css" href="styly.css">
    <title>Login</title>
  </head>

<body>
    <form>
          <input type="text" id="username" name="user_name" placeholder="Username"> <br>  <br>
          <input type="password" id="password" name="password" placeholder="Password"> <br>  <br>

          <input type="submit" value="Submit">
      </form>

<script>
      const url = "https://some/apo/endpoint/";
      const formEl = document.querySelector("form");
      formEl.addEventListener("submit", async (e) => {
        e.preventDefault();
        const formData = new FormData(formEl);
        const formDataSerialized = Object.fromEntries(formData);
        const jsonObject = {
          ...formDataSerialized,
          sendToSelf: formDataSerialized.sendToSelf ? true : false,
        };
        try {
          const response = await fetch(url, {
            method: "POST",
            body: JSON.stringify(jsonObject),
            headers: {
              "Content-Type": "application/json",

            },
            mode: "no-cors"
          });
          const json = await response.json();
          console.log(json);
        } catch (e) {
          console.error(e);
          alert("there as an error");
        }
      });
    </script>


  </body>
  </html>
Have you tried specifying a different Content-Type? I'm not sure what content type would be correct, but it's possible that is causing the HTTP 415 response. Also, check and make sure everything is correct in the jsonObject.
on the thought of his buddy zhuge qing, zhang chulan and feng baobao came to resolve the case with the help of "anywhere" business enterprise. how does zhang chulan display his competencies to assist the king trap the black hand backstage? what wonderful overall performance will zhuge qing, wang ye and others have? who're the people who covet the "8 wonders"? "under one guy-becoming a member of the world" may be introducedsoon!

voip phone system service provider