Using Python queries, can I add “data” to a prepared query?

The following code is set _requestif the HTTP method is GETthen to process PUT POSTand PATCHit is the operator if.

I am trying to install a single query installation statement for all types of methods.

Is it possible? It seems to me that there is no way to add datato the prepared request, and if this is true, then I may be stuck with the need for two different ways to configure the request, one way for GETand one way for PUT, PATCHand POST.

def fetch_from_api(self):
        s = Session()
        headers = { "Authorization" : REST_API_AUTHORIZATION_HEADER}
        _request = Request(self.method, self.url_for_api, headers=headers)

        if self.method in ['POST', 'PATCH', 'PUT']:
            headers['content-type'] = 'application/x-www-form-urlencoded'
            _request = Request(self.method, self.url_for_api, headers=headers, data=self.postdata)

        prepped = _request.prepare()
        self.api_response = s.send(prepped)
+4
source share
3 answers

requests.Request model, , data:

some_request = Request(method, url, headers=headers)
if # ...we decide we need to add data:
    some_request.data = data

, , , , , data.

:

, , , prepared_request. , prepared_request , prepare, ? , , , , , ?

, , , :

 def fetch_from_api(self):
    s = Session()
    headers = { "Authorization" : REST_API_AUTHORIZATION_HEADER}
    _request = Request(self.method, self.url_for_api, headers=headers)
    if self.method in ['POST', 'PATCH', 'PUT']:
        headers['content-type'] = 'application/x-www-form-urlencoded'
        _request.data = self.postdata
    prepped = _request.prepare()
    self.api_response = s.send(prepped)

( . ? fetch_from_api, POST ing PUT ing data. , , .)

: , , , , . , , , .

, , , , , , , , , , .

+2

HTTPforHumans, .

import requests

def pv_request(url, methods, data=None, headers=None, type=None):

    try:
        if 'POST' in methods:
            return requests.post(url=url, headers=headers, data=data).json()

        elif 'GET' in methods:
            return requests.get(url=url, headers=headers, data=data).json()

        elif 'PUT' in methods:
            if type == 'music':
                return requests.put(url=url, headers=headers, data=data).json()

            elif type == 'image':
                return requests.put(url=url, headers=headers, data=open(data, 'rb')).json()
    except requests.exceptions.ConnectionError:
        return None

, , " ".

+1

, , @DukeDougal . , .

, , , Request . , "POST", "PATCH" "PUT", Request , . . .

data= Request, data=None. , data Request ( PreparedRequest):

def fetch_from_api(self):
    s = Session()
    headers = {'Authorization': REST_API_AUTHORIZATION_HEADER}
    data = None  # Assume no data until method is checked

    if self.method in ['POST', 'PATCH', 'PUT']:
        headers['content-type'] = 'application/x-www-form-urlencoded'
        data = self.postdata  # Add the data

    # Now headers and data are ready, get a Request object
    _request = Request(self.method, self.url_for_api, headers=headers, data=data)

    prepped = _request.prepare()
    self.api_response = s.send(prepped)
0

All Articles