testing api sending OPTIONS method and getting 405 error

Viewed 45

Im new to vue and nuxt, im using this ones to connect them to my API made with fastapi, and whenever i try to create an account via my API with the vue form i get this error

127.0.0.1:52137 - "OPTIONS /user HTTP/1.1" 405 Method Not Allowed

I've seen that sometimes axios sends an OPTION method to "test" the api if i get it.. But how do i solve this problem ?

Im new to this so do not hesitate to ask me more files/code.

Here is the Post method that im trying to reach and my registration page on VUE.

@app.post("/user", response_model=_schemas.UserBis)
async def create_user(user: _schemas.UserIn, db: _orm.Session = fastapi.Depends(_services.get_db)):
  db_user_email = await _services.get_user_by_email(email=user.email, db=db)
  if db_user_email:
      raise fastapi.HTTPException(
          status_code=400, detail="User with that email already exists"
      )
  db_user_username = await _services.get_user_by_username(username=user.username, db=db)
  if db_user_username:
      raise fastapi.HTTPException(
          status_code=400, detail="User with that email already exists"
      )
  db_user_pseudo = await _services.get_user_by_pseudo(pseudo=user.pseudo, db=db)
  if db_user_pseudo:
      raise fastapi.HTTPException(
          status_code=400, detail="User with that pseudo already exists"
      )
  user = await _services.create_user(user=user, db=db)
  return _schemas.UserBis(data=user)

VUE:

<template>
    <section class="section">
      <div class="container">
        <div class="columns">
          <div class="column is-4 is-offset-4">
            <h2 class="title has-text-centered">Register!</h2>

            <Notification :message="error" v-if="error"/>

            <form method="post" @submit.prevent="register">
              <div class="field">
                <label class="label">Username</label>
                <div class="control">
                  <input
                    type="text"
                    class="input"
                    name="username"
                    v-model="username"
                    required 
                  />
                </div>
              </div>
              <div class="field">
                <label class="label">Pseudo</label>
                <div class="control">
                  <input
                    type="text"
                    class="input"
                    name="pseudo"
                    v-model="pseudo"
                    required
                  />
                </div>
              </div>
              <div class="field">
                <label class="label">Email</label>
                <div class="control">
                  <input
                    type="email"
                    class="input"
                    name="email"
                    v-model="email"
                    required
                  />
                </div>
              </div>
              <div class="field">
                <label class="label">Password</label>
                <div class="control">
                  <input
                    type="password"
                    class="input"
                    name="password"
                    v-model="password"
                    required
                  />
                </div>
              </div>
              <div class="control">
                <button type="submit" class="button is-dark is-fullwidth">Register</button>
              </div>
            </form>

            <div class="has-text-centered" style="margin-top: 20px">
              Already got an account? <nuxt-link to="/login">Login</nuxt-link>
            </div>
          </div>
        </div>
      </div>
    </section>
</template>

<script>
import Notification from '~/components/Notification'

export default {
    components: {
      Notification,
    },

    data() {
      return {
        username: '',
        pseudo: '',
        email: '',
        password: '',
        error: null
      }
    },

    methods: {
      async register() {
        try {
          await this.$axios.post('user', {
            username: this.username,
            pseudo: this.pseudo,
            email: this.email,
            password: this.password
          })

          await this.$auth.loginWith('local', {
            data: {
            email: this.email,
            password: this.password
            },
          })

          this.$router.push('/')
        } catch (e) {
          this.error = e.response.data.message
        }
      }
    }
}
</script>
2 Answers
Related