使用Django rest框架作为rest客户端 [英] Using Django rest framework as a rest client
本文介绍了使用Django rest框架作为rest客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个休息服务器,可以称为与:
I have a rest server that can be called e.g. with:
http://localhost:8080/myserver/rest/books/t1.json
我想知道如何使用 django rest框架作为客户端并调用我的休息服务器?
I want to know how can I use from django rest framework as a client and call my rest server?
推荐答案
Django Rest Framework用于创建 REST API,而不使用它们.要简单地从Django/Python调用REST API,您可以使用标准的 json
和 urllib2
执行以下操作:
Django Rest Framework is for creating REST APIs, not consuming them. To simply call a REST API from Django/Python you can do the following using json
and urllib2
which are standard:
import json
import urllib2
data = json.load(urllib2.urlopen('http://myapi.com/'))
或者您可以使用第三方库 请求
:
or you can use the 3rd party library requests
:
import requests
r = requests.get('http://myapi.com/')
r.json()
这篇关于使用Django rest框架作为rest客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文