Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Python requests, can I add "data" to a prepared request?

The code below sets up _request if the HTTP method is GET, then it has an ifstatement for handling PUT POST and PATCH.

I'm trying to have one single request setup statement for all method types.

Is this possible? It appears to me that there is no way to add data to a prepared request, and if this is true then perhaps I'm stuck with needing two different ways of setting up a request, one way for GET and one way for PUT, PATCH and 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)
like image 634
Duke Dougal Avatar asked Dec 11 '14 05:12

Duke Dougal


People also ask

What can you do with Python requests?

The requests module allows you to send HTTP requests using Python. The HTTP request returns a Response Object with all the response data (content, encoding, status, etc).

How do you send data in requests?

You can use several HTTP methods in an HTTP request. Each method sends data in the request in a different manner. For example, the GET method uses the query string of the RequestURI to pass parameter and value pairs. Other methods use the HTTP message body to send data in the request.

What is payload in Python request?

JSON Payload Example [Python Code] A request payload is data that clients send to the server in the body of an HTTP POST, PUT, or PATCH message that contains important information about the request.


1 Answers

The question is a little old and hopefully @DukeDougal already has a solution. Maybe this will help others, though.

The first thing I notice in the example is that a Request object is created near the beginning of the method. Then, if the method is "POST", "PATCH", or "PUT", the Request constructor is called again to get another object. In that case, the first object is gone. It was created unnecessarily.

When a data= argument isn't given to the Request constructor, it's the same as specifying data=None. Take advantage of that and call the constructor only once, then a data value won't need to be added to an existing Request (or PreparedRequest) object:

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)
like image 149
Mr. Lance E Sloan Avatar answered Sep 24 '22 15:09

Mr. Lance E Sloan