2

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

1 Answer 1

2

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)
  }
})
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Ok now it recognizes the media type, but the react native app doesn't recognize the response ;/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.