V
V
vladislavprozorovskiy2018-05-16 21:06:46
React
vladislavprozorovskiy, 2018-05-16 21:06:46

Why is a Post request sending a 415 error?

Good evening. There is such a request

import decode from 'jwt-decode';
export default class AuthService {
    constructor(domain) {
        
        this.fetch = this.fetch.bind(this)
        this.login = this.login.bind(this)
        this.getProfile = this.getProfile.bind(this)
    }

    login(username, password) {
        // Get a token
        return this.fetch('http://id.cryptofiles.org:2212/api/users/login/', {
            method: 'POST',
            mode:'no-cors',
            headers: {
                "Accept": "application/json; odata=nometadata",
                "Content-Type": "application/json;odata=nometadata",
                
              },
           'Content-Type': 'application/json',
            body: JSON.stringify({
                username,
                password
            })
        }).then(res => {
            this.setToken(res.auth_token)
            return Promise.resolve(res);
        })
    }

    loggedIn() {
        // Checks if there is a saved token and it's still valid
        const token = this.getToken()
        return !!token && !this.isTokenExpired(token) // handwaiving here
    }

    isTokenExpired(token) {
        try {
            const decoded = decode(token);
            if (decoded.exp < Date.now() / 1000) {
                return true;
            }
            else
                return false;
        }
        catch (err) {
            return false;
        }
    }

    setToken(idToken) {
        // Saves user token to localStorage
        localStorage.setItem('auth_token', idToken)
    }

    getToken() {
        // Retrieves the user token from localStorage
        return localStorage.getItem('auth_token')
    }

    logout() {
        // Clear user token and profile data from localStorage
        localStorage.removeItem('auth_token');
    }

    getProfile() {
        return decode(this.getToken());
    }


    fetch(url, options) {
        // performs api calls sending the required authentication headers
        const headers = {
            "Accept": "application/json; odata=nometadata",
            "Content-Type": "application/json;odata=nometadata",
            
        }

        if (this.loggedIn()) {
            headers['Authorization'] = 'Bearer ' + this.getToken()
        }

        return fetch(url, {
            headers,
            ...options
        })
            .then(this._checkStatus)
            .then(response => response.json())
    }

    _checkStatus(response) {
        // raises an error in case response status is not a success
        if (response.status >= 200 && response.status < 300) {
            return response
        } else {
            var error = new Error(response.statusText)
            error.response = response
            throw error
        }
    }
}

but the server sends 415 error, what could be the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mikhail Osher, 2018-05-16
@vladislavprozorovskiy

All the 415s I've seen have either a Content-Type header mismatch with the request body being passed, or just an unsupported Content-Type header.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question