使用 Django Rest Framework 向远程服务发出 post 请求



我想使用提供的端点URL将模态数据(JSON格式的每个条目的模态字段(从我的应用程序(由Python Django组成(发布到远程服务。

我想实现这样的事情,我尝试过但失败了

def modal_data_view(generics.ListAPIView):
    remote_service_response= requests.post(remote_url, data=my_modal_data)
    return HttpResponse(remote_service_response.text)

我希望远程终结点接收我的模态数据并返回文本响应。

如果您的远程服务支持 REST api,只需执行以下操作:

r = requests.post(remote_url,json=my_modal_data,headers={'Content-Type': 'application/json' })

此外,您的"my_modal_data"必须采用 JSON 格式。在 REST API 中,您可能需要身份验证在我的 github 中有一个 GET 和 POST 的例子,用于带有令牌身份验证的 REST api:

POST 到 REST 的示例

如果您的遥控器不使用 REST,您可能需要将数据放在 url(作为选项(或以远程服务格式的正文中