如何使用DjangoRestFramework UI中的字段发送请求 [英] How to send request using a field in DjangoRestFramework UI
问题描述
我正在从某个外部来源发出请求,我可以通过传递"reference_no"来手动成功发出请求.在我的网址中,但我希望有一种方法可以将"reference_no"在DRF用户界面中,然后单击将数据发送到外部端点并获取响应的帖子.
I am making a request from some external source, I am successfully able to make the request both manually by passing the "reference_no" in my url, but I will like to have a way whereby I can put the "reference_no" in the DRF UI and click on post which would send the data to the external endpoint and fetch the Response.
我不知道这是否可行,但我很乐意为您解决这个问题.
I don't know if this is possible but I would be glad to get a way across this.
views.py
class Pay(APIView):
def get(self, request, reference_id):
url = f"https://api.paystack.co/transaction/verify/{reference_id}"
payload = {}
files = {}
headers = {
'Authorization': 'Bearer SECRET_KEY',
'Content-Type': 'application/json'
}
response = requests.request("GET", url, headers=headers, data= payload, files=files)
return Response(response)
urls.py
from django.urls import path, include
from .views import *
urlpatterns = [
path('pay/<str:reference_id>', Pay.as_view(), name='pay'),
]
推荐答案
它应该可以工作,我会做一些小的更改:从响应( .json()
)获取数据以创建DRF响应:
It should work, I would do small change: get data from response (.json()
) to create DRF Response:
class Pay(APIView):
def get(self, request, reference_id):
url = f"https://api.paystack.co/transaction/verify/{reference_id}"
payload = {}
files = {}
headers = {
'Authorization': 'Bearer SECRET_KEY',
'Content-Type': 'application/json'
}
response = requests.request("GET", url, headers=headers, data= payload, files=files)
return Response(data=response.json()) # create DRF Response
在上面的示例中,没有错误处理,请添加它.
In the example above there is no error handling, please add it.
这篇关于如何使用DjangoRestFramework UI中的字段发送请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!