I am currently making a mobile app that fetch data from reddit API and display it on a remake of Reddit interface.
The application is made of multiple page the first being the login page, it use oauth 2.0 to retrieve a connection token and then navigate to another page where I try to get data about my reddit profile and display it.
The login page work as indented, I can connect to the reddit API and navigate to the next page but when I try to fetch any data from reddit API I got a 405 error code, sometime followed by an 500 error.
Here is the code for the backend of the second page :
const LoginScreen = ({navigation}) => {
let token = route.params.token;
const [username, setUsername] = useState('');
const [sub_count, setSubCount] = useState('');
const [profile_img_url, setImgUrl] = useState('');
const [date, setDate] = useState('');
async function fetchRedditAPI() {
var config = {
method: 'set',
url: 'https://oauth.reddit.com/api/v1/me',
headers: {
'Authorization': 'Bearer ' token
}
};
axios(config)
.then(function (response) {
console.log("----API DATA-----")
console.log(response["data"])
console.log("-----------------")
let profile_img_url = response["data"]["icon_img"]
setImgUrl(profile_img_url);
let date = response["data"]["created_utc"]
setDate(date);
let username = response["data"]["display_name_prefixed"]
setUsername(username);
let sub_count = response["data"]["subscribers"]
setSubCount(sub_count);
})
.catch(function (error) {
console.log("GOT THE FOLLOWING ERROR :")
console.log(error);
console.log("------------------------")
});
}
React.useEffect(() => {
console.log("fetching reddit API...");
fetchRedditAPI();
}, []);
The following error display in my terminal :
Request failed with status code 405
at node_modules\event-target-shim\dist\event-target-shim.js:818:20 in EventTarget.prototype.dispatchEvent
at node_modules\react-native\Libraries\Network\XMLHttpRequest.js:605:6 in setReadyState
at node_modules\react-native\Libraries\Network\XMLHttpRequest.js:395:6 in __didCompleteResponse
at node_modules\react-native\Libraries\vendor\emitter\EventEmitter.js:189:10 in emit
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:416:4 in __callFunction
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:109:6 in __guard$argument_0
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:364:10 in __guard
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:108:4 in callFunctionReturnFlushedQueue
Request failed with status code 500
at node_modules\event-target-shim\dist\event-target-shim.js:818:20 in EventTarget.prototype.dispatchEvent
at node_modules\react-native\Libraries\Network\XMLHttpRequest.js:605:6 in setReadyState
at node_modules\react-native\Libraries\Network\XMLHttpRequest.js:395:6 in __didCompleteResponse
at node_modules\react-native\Libraries\vendor\emitter\EventEmitter.js:189:10 in emit
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:416:4 in __callFunction
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:109:6 in __guard$argument_0
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:364:10 in __guard
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:108:4 in callFunctionReturnFlushedQueue
Any help would be precious, thank you in advance !
CodePudding user response:
A 405 error occurs when you try to access an endpoint using a request method that is not allowed. I assume it's because your request method is set
instead of get
?