Simple HTTP GET Request mit Headers?

07/11/2022 15:53 BlueBasHeR#1
Hey, vorab ich hab die SuFu genutzt aber aus verzweifelung copy & paste ich alles durch und bekomme kein ergebnis...


Mein Projekt:
Ich lasse auf meinem IPhone Siri Shortcuts laufen, an sich funktioniert alles und zwar handelt es sich einfach um eine Bildersuche, wo dann mehrere Bilder angezeigt werden und man soll dann mit einem Submit Button pro bild quasi ein Like geben können.

auf dem IPhone in der Kurzbefehle app mit "Inhalte von Website" abrufen "Methode: GET" und 4 Headers die man immer Benötigt!
1. X-AUTH-TOKEN = (Mein Token ist Vorhanden.)
2. Content-Type = Application/json
3. Accept = Application/json
4. Platform = Web
funktioniert alles Perfekt.

Wieso ich dann alles über JS machen möchte?
Weil ich im IPhone mit JS und HTML eine Locale Website anzeigen kann die man dann Gestaltet wie man möchte und der Code ist definitiv viel Kürzer...

Nochmal in Kurzform:
Ich brauche irgend eine Möglichkeit JS in einem Simplen HTTP Code auszuführen in einem "<script></script>" Block. (Ja es funktioniert in der Kurzbefehle app :D !)

Xhr oder Fetch eigentlich egal muss nur
GET [Only registered and activated users can see links. Click Here To Register...] auch vorhanden)
Header: oben aufgelistet..

danke im vorraus, ich weiss ich hab es umständlich geschildert, bin aufm weg zur arbeit :D
07/20/2022 23:05 Valomirant#2
Schau dir Fetch an o:
08/05/2022 13:47 Wurzelhüpfer#3
[Only registered and activated users can see links. Click Here To Register...]
08/05/2022 14:00 R3D*#4
Did you try to use fetch method?
08/05/2022 16:24 Hyukisawa#5
Mit fetch [Only registered and activated users can see links. Click Here To Register...]

Code:
const fetchURL = async (url) => {
    const response = await fetch(url, {
        method: "GET",
        headers: {
            "X-AUTH-TOKEN": "myToken",
            "Content-Type": "Application/json",
            "Accept": "Application/json",
            "Platform": "Web"
        }
    })
    const data = await response.json();

    // do stuff with data;
    
}

fetchURL("https://api.website.com/like/123456789");
02/07/2023 16:54 akatema92#6
Here is an example of a simple HTTP GET request with headers using Python:
makefile
import requests url = "https://www.example.com" headers = { "User-Agent": "My User Agent", "Accept-Language": "en-US" } response
05/09/2023 18:51 gulshan212#7
Hello this is Gulshan Negi
Well, to execute JavaScript in a simple HTTP code, you can use the Fetch API. Here's an example code snippet that shows how you can use Fetch to make a GET request to the URL you provided with the headers you listed:

fetch('https://api.website.com/like/123456789', {
method: 'GET',
headers: {
'X-AUTH-TOKEN': 'your-token-value',
'Content-Type': 'application/json',
'Accept': 'application/json',
'Platform': 'Web'
}
})
.then(response => {
if (response.ok) {
return response.json();
} else {
throw new Error('Network response was not ok.');
}
})
.then(data => {
console.log(data); // handle response data here
})
.catch(error => {
console.error('Error:', error);
});

Thanks