So beitragen Sie Daten aus zwei API in Python
Posted: 08 May 2025, 14:50
Also, weit habe ich nur Get/Post -Daten von einer API gleichzeitig verwendet. Ich bin mir nicht sicher, wie ich Daten aus zwei APIs beitreten kann. Ähnlich wie SQL gibt es eine Art Join -Tabellen, die mit der API mit der API zu erreichen sind?
Danke.
Code: Select all
API 1
https://test.com/api/Staff/GetStaffDetails
StaffId, Firstname, Lastname, Email
API 2
https://test.com/api/Staff/GetStaffPhotos
Id (this matches with the StaffId), Photo
< /code>
Mitarbeiterdetails < /p>
def StaffDetails(request, *, context):
url = "https://test.com/api/Staff/GetStaffDetails"
headers = {
"X-DOMAIN": "test",
"X-TOKEN": "123456789qwertyuiop",
"User-Agent": "",
}
response = requests.post(url, headers=headers)
data = response.json()
random_data = random.sample(data, min(5, len(data)))
context = {"data": random_data}
return render(request, "StaffDetails.html", context)
StaffDetails.html
STAFFID
FIRSTNAME
LASTNAME
EMAIL
{% for row in data %}
{{ row.StaffId }}
{{ row.Firstname }}
{{ row.Lastname }}
{{ row.Email }}
{% endfor %}
< /code>
Mitarbeiterfoto < /p>
def StaffPhoto(request, *, context):
url = "https://test.com/api/Staff/GetStaffPhotos"
headers = {
"X-DOMAIN": "test",
"X-TOKEN": "123456789qwertyuiop",
"User-Agent": "",
}
response = requests.post(url, headers=headers)
data = response.json()
random_data = random.sample(data, min(5, len(data)))
context = {"data": random_data}
return render(request, "StaffPhoto.html", context)
StaffPhoto.html
ID
PHOTO
{% for row in data %}
{{ row.Id }}
[img]data:image/jpeg;base64,{{ row.Photo }}[/img]
{% endfor %}