我一直在使用Django和Djangorest框架编程API,以便在Google App Engine中部署。API基本上是一个包注册表,因此您可以使用API创建、更新、获取和删除包。
所有endpoint似乎都工作,除了一个。唯一不起作用的终结点是显示在线注册表中所有包的分页列表的终结点。
所有endpoint都在工作,但由于某种原因,当我点击特定的endpoint“/packages/”时,GCP给出了错误消息
400. That’s an error.
Your client has issued a malformed or illegal request. That’s all we know
当我在计算机上本地运行应用程序时,所有endpoint都能正常工作。当我在Google App Engine上部署该应用程序时,该应用程序仅停止对该特定路由的工作。API有效载荷应为:
[{“名称”:“*”}]
我完全迷失在这个问题上,如果有任何帮助,我将不胜感激。
意见。派克
import django.db.utils
from rest_framework.decorators import api_view
from rest_framework.response import Response
from django.core.paginator import Paginator, EmptyPage
import registry.models
from .serializers import *
from .models import *
# Create your views here.
@api_view(['GET'])
def apiOverview(request):
api_urls = {
'List': '/package-list/',
'Create': '/package-create/',
'Update': '/package-update/',
'Delete': '/package-delete/',
'Rate': '/package-rate/',
'Download': '/package-download/'
}
return Response(api_urls)
@api_view(['GET'])
def packages_middleware(request):
print(request)
print(type(request))
print(request.data)
print(type(request.data))
# determine offset query parameter
offset = request.GET.get('Offset')
if offset is None:
offset = 0
else:
offset = int(offset)
# capturing request body
response = []
queries = request.data
if len(queries) < 1:
return Response({'message: empty request body array'}, status=400)
else:
if len(queries) == 1 and queries[0]['Name'] == '*':
response = list(PackageMetadata.objects.all().values())
else:
for query in queries:
if 'Name' in query.keys() and 'Version' in query.keys():
for x in list(PackageMetadata.objects.filter(Name__icontains=query['Name']).filter(
Version__contains=query['Version']).values()):
response.append(x)
else:
response.append({
'message': 'query {q} is missing at least one attribute - remember to check spelling and capitalization'.format(q=query)
})
paginator = Paginator(response, 10)
try:
return Response(paginator.page(offset + 1).object_list, headers={'Offset': offset + 1})
except EmptyPage:
return Response(paginator.page(1).object_list, headers={'Offset': 2})
@api_view(['GET', 'PUT', 'DELETE'])
def package_middleware(request, pk):
try:
package = Package.objects.get(Metadata__ID__exact=str(pk))
if request.method == 'GET':
# CHECK THAT CONTENT FIELD IS SET
serializer = PackageSerializer(package, many=False)
return Response(serializer.data, status=200)
elif request.method == 'PUT':
payload = request.data
if 'Metadata' in payload and 'Data' in payload:
if payload['Metadata'] != PackageMetadataSerializer(package.Metadata, many=False).data:
return Response({"message": "name, version and ID must match"}, status=400)
else:
# CHECK THAT ONLY ONE DATA FIELD IS SET
serializer_data = PackageDataSerializer(data=payload['Data'], many=False)
if serializer_data.is_valid(raise_exception=True):
try:
serializer_data.update(instance=package.Data, validated_data=serializer_data.validated_data)
except django.db.utils.IntegrityError:
return Response(
{"message": "both Content and URL must be included in query, but exactly one can be set"},
status=400)
return Response(status=200)
else:
return Response(
{"message": "incorrect request body schema - remember to check spelling and capitalization"},
status=400)
else:
package.Metadata.delete()
package.Data.delete()
package.delete()
return Response({"message": "package deleted"}, status=200)
except registry.models.Package.DoesNotExist:
return Response({"message": "package not found"}, status=400)
@api_view(['POST'])
def create_package_middleware(request):
payload = request.data
print(payload)
if 'Metadata' in payload and 'Data' in payload:
serializer_metadata = PackageMetadataSerializer(data=payload['Metadata'], many=False)
serializer_data = PackageDataSerializer(data=payload['Data'], many=False)
if serializer_data.is_valid(raise_exception=True) and serializer_metadata.is_valid(raise_exception=True):
try:
metadata = PackageMetadata.objects.create(ID=serializer_metadata.data.get('ID'),
Name=serializer_metadata.data.get('Name'),
Version=serializer_metadata.data.get('Version'))
except django.db.utils.IntegrityError:
return Response({"message": "duplicate key-value (Name, Version) violates uniqueness constraint"},
status=403)
try:
data = PackageData.objects.create(Content=serializer_data.data.get('Content'),
URL=serializer_data.data.get('URL'))
except django.db.utils.IntegrityError:
metadata.delete()
return Response(
{"message": "both Content and URL must be included in query, but exactly one can be set"},
status=400)
Package.objects.create(Data=data, Metadata=metadata)
serializer_metadata = PackageMetadataSerializer(metadata, many=False)
return Response(serializer_metadata.data, status=200)
else:
return Response({"message": "incorrect request body schema - remember to check spelling and capitalization"},
status=400)
@api_view(['DELETE'])
def byName_middleware(request, name):
if name == '*':
return Response({"message": "query name reserved"}, status=400)
querySet = Package.objects.filter(Metadata__Name__iexact=name)
if len(querySet) == 0:
return Response({"message": "package not found"})
else:
for package in querySet:
package.Metadata.delete()
package.Data.delete()
package.delete()
return Response(status=200)
网址。派克
from django.urls import path, include
from . import views
urlpatterns = [
path('', views.apiOverview),
path('packages/', views.packages_middleware, name='packages_middleware'),
path('package/<str:pk>', views.package_middleware, name='package'),
path('package/', views.create_package_middleware, name='create'),
path('package/byName/<str:name>', views.byName_middleware, name='byName')
]
Py
from django.db import models
# Create your models here.
class PackageData(models.Model):
Content = models.TextField(blank=True, null=True) # actual zip file
URL = models.CharField(max_length=500, blank=True, null=True) # url of package
# class Meta:
# constraints = [
# models.CheckConstraint(
# name="%(app_label)s_%(class)s_content_or_url",
# check=models.Q(Content__isnull=True, URL__isnull=False) | models.Q(Content__isnull=False, URL__isnull=True)
# )
# ]
class PackageMetadata(models.Model):
class Meta:
constraints = [
models.UniqueConstraint(fields=['Name', 'Version'], name='unique_package')
]
ID = models.CharField(primary_key=True, max_length=50)
Name = models.CharField(max_length=50)
Version = models.CharField(max_length=50)
class Package(models.Model):
Data = models.ForeignKey(PackageData, on_delete=models.CASCADE)
Metadata = models.ForeignKey(PackageMetadata, on_delete=models.CASCADE)
class PackageRating(models.Model):
BusFactor = models.DecimalField(max_digits=10, decimal_places=9)
Correctness = models.DecimalField(max_digits=10, decimal_places=9)
GoodPinningPractice = models.DecimalField(max_digits=10, decimal_places=9)
LicenseScore = models.DecimalField(max_digits=10, decimal_places=9)
RampUp = models.DecimalField(max_digits=10, decimal_places=9)
ResponsiveMaintainer = models.DecimalField(max_digits=10, decimal_places=9)
class PackageQuery(models.Model):
Name = models.CharField(max_length=50)
Version = models.CharField(max_length=50)
您正在对路由定义packages_middleware(请求)进行GET
调用。
在该函数中,您有querys=request。数据
和其他逻辑依赖于查询
。本文档说明了请求。数据
用于POST、补丁和PUT。如果这是正确的,那么这似乎就是你的错误所在。
您可以在该行前后放置一条打印语句,然后查看应用程序是否在该行之后打印该语句。这将帮助您确认这是否是问题所在。
您还应该打印查询的内容
。由于GET请求实际上没有发送正文,因此可能会导致其余逻辑失败,因为querys
的值不是您期望的值。
我有一个函数,使用CURL和谷歌地图应用编程接口返回地址的坐标。 代码如下: 代码对我和我使用它的99%的网络服务器都很好,但是对于大约1%的服务器,谷歌会返回错误消息: 我已经检查过了,googleapi键是正确的,PHP CURL已启用,PHP版本与它正在使用的PHP版本匹配。 有人能想到其他可能导致谷歌返回此消息的因素吗?
drupal_http_request send“: "
我正在获取一条消息,通过gmail API获取完整消息: 我正在传递一条有效的消息。Id号 谷歌。GoogleApiException:“谷歌。API。请求。请求错误 找不到[404] 谷歌。应用程序。请求。请求错误未找到[404]错误[消息[未找到]位置[-]原因[未找到]域[全局]] 这里还有一个问题 有人遇到过这个吗?
我正试图从我的网站下载一个HTTPS页面,该页面由SNI托管在谷歌应用程序引擎上。无论我使用什么库,都会出现以下错误: 我尝试了多种方法来解决此错误,包括使用urllib3 openssl monkeypatch: 但是我总是得到上面提到的同样的错误。 有什么想法吗?
我正在尝试获取youtube搜索结果的页面。在这里,我输入了一个视频名称,我正在尝试获取搜索结果的html页面。 我使用http模块发送和接收数据。 这是我收到的结果页 我不能理解错误的网址的原因
YouTube官方网站经常出错。如果我尝试,它在匿名模式下工作。这说明问题是由浏览器上的cookie/cache/localstorage/application数据引起的。 如果我删除数据,它将再次开始工作,但是,我将需要在我使用的所有gmail帐户中再次登录。 我已经要求谷歌(在反馈和推特上)对此进行调查,但他们无能为力。 因此,问题是: 如果有人遇到过这个问题,您是否已经了解了为什么以及如何