如何发送请求?

时间:2019-05-15 15:00:29

标签: django django-models django-rest-framework

我需要发送以下数据的请求

"order": {
   "server_callback_url": "http://site.id/callback",
   "currency": "UAH",
   "amount": "1400",
   "order_type": "settlement",
   "response_url": "http://site.id/test/responsepage/",
   "order_id": "test1234561467462099.19",
   "operation_id": "test1234561467462099.19",
   "order_desc": "test order",
   "merchant_id": 700001,
   "receiver": [
     {
       "requisites": {
         "amount": 100,
         "merchant_id": 500001
       },
       "type": "merchant"
     },{
       "requisites": {
         "amount": 200,
         "merchant_id": 600001
       },
       "type": "merchant"
     },
  ]
}

我需要将它们发送到https://api.fondy.eu/api/settlement

但是我从来没有那样做。我一点都不熟悉DRF。请告诉我如何实施。

2 个答案:

答案 0 :(得分:2)

您可以使用DRF作为文档: https://www.django-rest-framework.org/tutorial/2-requests-and-responses/

或者不带DRF:

# importing the requests library 
import requests 

# api-endpoint 
URL = "https://api.fondy.eu/api/settlement"


# defining a params dict for the parameters to be sent to the API 
 data =
   "order": {
   "server_callback_url": "http://site.id/callback",
   "currency": "UAH",
   "amount": "1400",
   "order_type": "settlement",
   "response_url": "http://site.id/test/responsepage/",
   "order_id": "test1234561467462099.19",
   "operation_id": "test1234561467462099.19",
   "order_desc": "test order",
   "merchant_id": 700001,
   "receiver": [
     {
       "requisites": {
         "amount": 100,
         "merchant_id": 500001
       },
       "type": "merchant"
     },{
       "requisites": {
         "amount": 200,
         "merchant_id": 600001
       },
       "type": "merchant"
     },
  ]
}

# sending get request and saving the response as response object 
r = requests.POST(url = URL, data= data) 

# extracting data in json format 
data = r.json() 

也许您想在编写代码之前尝试API,有postman之类的工具可以快速完成此操作:)

答案 1 :(得分:2)

如果访问端点,则表明仅允许POST方法。我们可以使用requests package发出POST请求。

import requests

data = {
  "order": {
     "server_callback_url": "http://site.id/callback",
     "currency": "UAH",
     "amount": "1400",
     "order_type": "settlement",
     "response_url": "http://site.id/test/responsepage/",
     "order_id": "test1234561467462099.19",
     "operation_id": "test1234561467462099.19",
     "order_desc": "test order",
     "merchant_id": 700001,
     "receiver": [
       {
         "requisites": {
         "amount": 100,
         "merchant_id": 500001
       },
       "type": "merchant"
     },{
       "requisites": {
           "amount": 200,
           "merchant_id": 600001
       },
       "type": "merchant"
     },
    ]
  }
}

headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
response = requests.post('https://api.fondy.eu/api/settlement', data=data, headers=headers)

header可能很重要,因为它会告诉您您要求的格式,并且如果您未正确设置Content-type,某些API将无法正常工作。

response在这里是Response对象,您可以使用以下方法解析对Python字典的响应:

response.json()

这在本地给了我

{'response': {'error_code': 1002,
              'error_message': 'Application error',
              'request_id': 'iUxQzJfyBuxdI'}}

状态码为200,因此可能意味着您指定的回调无效(或请求中的其他一些项)。