Как отправить запрос? - PullRequest
2 голосов

Мне нужно отправить запрос со следующими данными

"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 ]

2 голосов
/ 15 мая 2019

Если мы посещаем конечную точку, она говорит, что разрешены только методы POST.Мы можем сделать POST-запрос, например, с помощью пакета requests .

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 может быть важным, поскольку оно говорит вам, чтоФормат вы делаете запрос, и некоторые API не работают, если вы не установите Content-type должным образом.

response здесь является Response объектом, и вы можете проанализировать ответ на Pythonсловарь с:

response.json()

локально это дает мне:

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

Код состояния - 200, так что, вероятно, это означает, что указанный вами обратный вызов недействителен (или некоторые другие элементы вВаш запрос).

2 голосов
/ 15 мая 2019

Вы можете использовать DRF в качестве документации: https://www.django -rest-framework.org / tutorial / 2-запросы-и-ответы /

Или без 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 перед написанием кода, есть инструмент, подобный почтальон , чтобы сделать это быстро:)

...