Search code examples
djangoreact-nativedjango-rest-frameworkdjango-rest-auth

Problem with django rest auth and react native


I am trying to make a login page with a react native frontend and a django-rest-auth backend, but when I send my request:

`login = () => {

fetch('http://myIp/rest-auth/login/', {
  method: 'POST',
  header: {
    'Accept': 'application/json',
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    username: this.state.username,
    password: this.state.password,
  })
})
.then((response) => response.json())
.then((res) => {
  if (res.success === true) {
    AsyncStorage.setItem('user', res.user)
    this.props.navigation.navigate('Profile')
  }
  else {
    alert(res.message)
  }
})
  }
}`

My backend returns:

Unsupported Media Type

I only use django-rest-auth api endpoints. What is the problem ? Thank you for your help ;D


Solution

  • In above code you have used header instead headers. Please change your code like below.

    login = () => {
    
    fetch('http://myIp/rest-auth/login/', {
      method: 'POST',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({
        username: this.state.username,
        password: this.state.password,
      })
    })
    .then((response) => response.json())
    .then((res) => {
      if (res.success === true) {
        AsyncStorage.setItem('user', res.user)
        this.props.navigation.navigate('Profile')
      }
      else {
        alert(res.message)
      }
    })
      }
    }