Guest Posted March 23, 2022 Share Posted March 23, 2022 Hello everyone, I'm trying to do an "GET" Request on the TruckersMP API to get the amount of Members in an VTC But I have no idea on how to do that so... How can I do an "GET" Request on the TruckersMP API to get the amount of Members in an VTC and display it on my website? Need an way to add it to HTML! Link to comment Share on other sites More sharing options...
CJMAXiK Posted March 29, 2022 Share Posted March 29, 2022 8 hours ago, InfoBlock said: Perfect, no one can help me... You just need to read TruckersMP API documentation - https://stats.truckersmp.com/api#vtc_members. TruckersMP provides an API, other stuff like processing and templating is up to you. Link to comment Share on other sites More sharing options...
Guest Posted March 29, 2022 Share Posted March 29, 2022 14 hours ago, CJMAXiK said: You just need to read TruckersMP API documentation - https://stats.truckersmp.com/api#vtc_members. TruckersMP provides an API, other stuff like processing and templating is up to you. Well I have no idea how to do an API Request I've never worked with API's on a Website Link to comment Share on other sites More sharing options...
[TPH] Nicz Posted April 11, 2022 Share Posted April 11, 2022 Sounds good if you will follow the documentation it's simple to get. Link to comment Share on other sites More sharing options...
Bobita Carciumaru Posted April 27, 2022 Share Posted April 27, 2022 did you try to google something? or some tutorials? Link to comment Share on other sites More sharing options...
Bеаn Posted May 1, 2022 Share Posted May 1, 2022 Hello, If you want to add data from the API to your web application, you can use this. If the aforementioned isn't suitable, you can use tutorials like this to aid you in creating what you desire. Please note that the above are not solutions and are simply to aid you in the process of creating what you need. Kind Regards, Bean Link to comment Share on other sites More sharing options...
Guest Posted May 4, 2022 Share Posted May 4, 2022 I need an way to do it in JavaScript not in PHP, I have 0 PHP experience! Link to comment Share on other sites More sharing options...
CJMAXiK Posted May 4, 2022 Share Posted May 4, 2022 3 hours ago, InfoBlock said: I need an way to do it in JavaScript not in PHP, I have 0 PHP experience! There are plenty of public API usage tutorials on Internet, I do not understand why we have to make the thing specifically for you? Link to comment Share on other sites More sharing options...
Guest Posted May 5, 2022 Share Posted May 5, 2022 On 5/4/2022 at 11:21 AM, CJMAXiK said: There are plenty of public API usage tutorials on Internet, I do not understand why we have to make the thing specifically for you? Some real examples of an API Request via JavaScript would be good not just for example GET /player/{id} Link to comment Share on other sites More sharing options...
Guest Posted May 5, 2022 Share Posted May 5, 2022 On 5/1/2022 at 11:47 PM, Trucker_Bean said: Hello, If you want to add data from the API to your web application, you can use this. If the aforementioned isn't suitable, you can use tutorials like this to aid you in creating what you desire. Please note that the above are not solutions and are simply to aid you in the process of creating what you need. Kind Regards, Bean The second tutorial kinda helps me out but I keep getting an failed to fetch error Link to comment Share on other sites More sharing options...
Bеаn Posted May 5, 2022 Share Posted May 5, 2022 3 hours ago, InfoBlock said: The second tutorial kinda helps me out but I keep getting an failed to fetch error If you're getting errors, there're plenty of platforms for you to reach out to and get help. Link to comment Share on other sites More sharing options...
Guest Posted May 7, 2022 Share Posted May 7, 2022 On 5/5/2022 at 11:07 PM, Trucker_Bean said: If you're getting errors, there're plenty of platforms for you to reach out to and get help. I've been looking for hours and found absolutely nothing, even looked at some Discord Bots doing API Requests to know how it works as I only had to make it available for Web use then, still nothing Link to comment Share on other sites More sharing options...
Bеаn Posted May 7, 2022 Share Posted May 7, 2022 1 hour ago, InfoBlock said: I've been looking for hours and found absolutely nothing, even looked at some Discord Bots doing API Requests to know how it works as I only had to make it available for Web use then, still nothing For example, this is how I'd create a request in python. import requests import json query = requests.get('url') result = query.json() You can use the API documentation should you require more help or want to know what data you can receive from endpoints etc... While I understand you cannot find a solution to work for you, you shouldn't expect someone to spoon feed you a solution. If you go forward with this mindset, you'll not learn for yourself. A key part of programming is problem-solving and having the ability to overcome any roadblocks you may encounter. I hope this helps. Kind Regards, Bean Link to comment Share on other sites More sharing options...
Guest Posted May 7, 2022 Share Posted May 7, 2022 5 hours ago, Trucker_Bean said: For example, this is how I'd create a request in python. import requests import json query = requests.get('url') result = query.json() You can use the API documentation should you require more help or want to know what data you can receive from endpoints etc... While I understand you cannot find a solution to work for you, you shouldn't expect someone to spoon feed you a solution. If you go forward with this mindset, you'll not learn for yourself. A key part of programming is problem-solving and having the ability to overcome any roadblocks you may encounter. I hope this helps. Kind Regards, Bean Well I found some useful stuff on the internet but the requests keep resulting in an "Failed to fetch" which I can't seem to get fixed.. Link to comment Share on other sites More sharing options...
Bеаn Posted May 8, 2022 Share Posted May 8, 2022 Well, I'm glad you've managed to find something on the internet. On 5/7/2022 at 8:46 AM, InfoBlock said: but the requests keep resulting in an "Failed to fetch" which I can't seem to get fixed. While examples on the internet may work for their intended purpose, they may not work for your intended purpose. Therefore, you'd have to alter the pre-provided examples to work with the API and what you require. Link to comment Share on other sites More sharing options...
Guest Posted May 11, 2022 Share Posted May 11, 2022 On 5/8/2022 at 9:19 PM, Trucker_Bean said: Well, I'm glad you've managed to find something on the internet. While examples on the internet may work for their intended purpose, they may not work for your intended purpose. Therefore, you'd have to alter the pre-provided examples to work with the API and what you require. I can't work with the given examples as those aren't real code examples! It is literally just GET [Some API Link Request] and then shows the result underneath Link to comment Share on other sites More sharing options...
Bеаn Posted May 11, 2022 Share Posted May 11, 2022 Isn't that what you want? Once you've got the result of your request, you can use that how you like. Link to comment Share on other sites More sharing options...
Guest Posted May 14, 2022 Share Posted May 14, 2022 On 5/11/2022 at 4:02 AM, Trucker_Bean said: Isn't that what you want? Once you've got the result of your request, you can use that how you like. No? I need some real examples on how to do the GET request! Everything I tried resulted in "Failed to fetch" Link to comment Share on other sites More sharing options...
Coolio85 Posted May 15, 2022 Share Posted May 15, 2022 @InfoBlock Would you be able to post the code you are trying to run then someone might be able to help you? Link to comment Share on other sites More sharing options...
Guest Posted May 15, 2022 Share Posted May 15, 2022 2 hours ago, Coolio85 said: @InfoBlock Would you be able to post the code you are trying to run then someone might be able to help you? let url = 'https://api.truckersmp.com/v2/player/2515622'; fetch(url).then(function(response) { return response.json(); }).then(function(data) { console.log(data); }).catch(function() { console.log("Catch Log"); }); Would've tried it with PHP but my Websites run via Github and on Github PHP doesn't work at all Link to comment Share on other sites More sharing options...
Guest Posted May 23, 2022 Share Posted May 23, 2022 What am I doing wrong??? I tried so many different codes now! This is the last code that I tried async function getUser() { try { const response = await fetch('https://api.truckersmp.com/v2/player', { method: 'GET', headers: { accept: 'application/json', mode: 'no-cors', }, }); if (!response.ok) { throw new Error(`[ERROR]: Status: ${response.status}`); } const result = await response.json(); return result; } catch (err) { console.log(err); } } getUser(); Link to comment Share on other sites More sharing options...
CJMAXiK Posted May 23, 2022 Share Posted May 23, 2022 This is because of CORS (browser security), you need to use a server part (PHP, Node.js or other) OR any services that can bypass CORS. Link to comment Share on other sites More sharing options...
User_2416306 Posted May 24, 2022 Share Posted May 24, 2022 try Stack Overflow. Its a well known site that can assist with programming. Be aware though, they wont help people who just want it made for them, theyll give you all the resources, explain why its doing what and how to get around it etc etc, but they arent there to give hand outs, so be as detailed as you can with what you are trying to do, what you've tried, and what is happening. Goodluck Link to comment Share on other sites More sharing options...
Guest Posted May 24, 2022 Share Posted May 24, 2022 On 5/23/2022 at 5:22 PM, CJMAXiK said: This is because of CORS (browser security), you need to use a server part (PHP, Node.js or other) OR any services that can bypass CORS. I managed to bypass the CORS Browser Security by using Node.js as a server part, the API request is working just fine now on an localhost but it stops working once I try to import the js into HTML using the script tag It keeps saying me "require is not defined" or "module is not defined" - Now I need an solution for that Link to comment Share on other sites More sharing options...
CJMAXiK Posted May 25, 2022 Share Posted May 25, 2022 21 hours ago, InfoBlock said: Now I need an solution for that You need to serve the server part online and use it as an endpoint for your HTML script part. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.