嘿,伙计们当我调用DRF端点时,我试图从第三方API中摄取一些数据。
我可以使用urllib2轻松构建get请求并反序列化json,但有没有办法使用django序列化程序并以drf方式执行?或者我只是想过这个?
import json, urllib2
class IngestThirdPartyView(APIView):
def post(self, request):
data = request.data
url = 'https://third_party.com/media/{}'.format(data['item_id]')
response = urllib.urlopen(url)
# Handle item somehow
item = json.load(response)
非常欢迎任何有关DRF友好方式处理此问题的建议!
干杯
答案 0 :(得分:0)
从您的示例代码中
import requests
class IngestThirdPartyView():
def post(self, request):
data = request.data
url = 'https://third_party.com/media/{}'.format(data['item_id]')
item = requests.get(url).json() # this will retrieve json response
# then you could deserialize the above json and validate it or something
# as described here in docs http://www.django-rest-framework.org/api-guide/serializers/#deserializing-objects
# Handle item somehow