Jump to content

Help needed regarding API


Guest InfoBlock
 Share

Recommended Posts

Guest InfoBlock

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

Guest InfoBlock
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

  • 2 weeks later...
  • 3 weeks later...

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

Guest InfoBlock

I need an way to do it in JavaScript not in PHP, I have 0 PHP experience!

Link to comment
Share on other sites

Guest InfoBlock
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

Guest InfoBlock
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

Guest InfoBlock
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

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

Guest InfoBlock
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

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

Guest InfoBlock
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

Guest InfoBlock
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

Guest InfoBlock
Posted (edited)
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

Edited by InfoBlock
Added the Code
Link to comment
Share on other sites

Guest InfoBlock

image.png.564115e29869f118f184ef1db62a1382.png

 

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

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

5

Link to comment
Share on other sites

Guest InfoBlock
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

Guest
This topic is now closed to further replies.
 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.