How to make a post request with the Python requests library? How to make a post request with the Python requests library? django django

How to make a post request with the Python requests library?


Your Postman request is a JSON body. Just reproduce that same body in Python. Your Python code is not sending JSON, nor is it sending the same data as your Postman sample.

For starters, sending a dictionary via the data arguments encodes that dictionary to application/x-www-form-urlencoded form, not JSON. Secondly, you appear to be sending a single filter.

The following code replicates your Postman post exactly:

import requestsfilters = {"filter": {    "filters": [{        "field": "RCA_Assigned_Date",        "operator": "gte",        "value": "2017-05-31 00:00:00"    }, {        "field": "RCA_Assigned_Date",        "operator": "lte",        "value": "2017-06-04 00:00:00"    }, {        "field": "T_Subcategory",        "operator": "neq",        "value": "Temporary Degradation"    }, {        "field": "Issue_Status",        "operator": "neq",        "value": "Queued"    }],    "logic": "and"}}url = "http://10.61.202.98:8081/T/a/api/rows/cat/ect/tickets"response = requests.post(url, json=filters)

Note that filters is a Python data structure here, and that it is passed to the json keyword argument. Using the latter does two things:

  • Encode the Python data structure to JSON (producing the exact same JSON value as your raw Postman body value).
  • Set the Content-Type header to application/json (as you did in your Postman configuration by picking the JSON option in the dropdown menu after picking raw for the body).

requests is otherwise just an HTTP API, it can't make Cassandra do any more than any other HTTP library. The urllib.request.urlopen code sends GET requests, and are trivially translated to requests with:

def get_json():    url = "http://10.61.202.98:8081/T/a/api/rows/cat/ect/tickets"    response = requests.get(url, params={'user_name': user}, timeout=15)        return response.json()

I removed the if branching and replaced that with using the params argument, which translates a dictionary of key-value pairs to a correctly encoded URL query (passing in the user name as the user_name key).

Note the json() call on the response; this takes care of decoding JSON data coming back from the server. This still takes long, you are not filtering the Cassandra data much here.


I would recommend using the json attribute instead of data. It handles the dumping for you.

import requestsdata = {'user_name':'user&001'}headers = {'Content-Type': 'application/json', 'Accept': 'application/json'}url = "http://10.61.202.98:8081/T/a/api/rows/cat/ect/tickets/"r = requests.post(url, headers=headers, json=data)

Update, answer for question 3. Is there a reason you are using urllib? I’d use python requests as well for this request.

import requestsdef get_json():    r = requests.get("http://10.61.202.98:8081/T/a/api/rows/cat/ect/tickets”, params={"user_name": user_name.replace(" ", "&")})    return r.json# not sure what you’re doing here, more context/code example would helpdef get_tickets_not_temp_degradation(start_date, end_date, complete_):    return Counter([k['user_name'] for k in complete_data if start_date < dateutil.parser.parse(k.get('DateTime')) < end_date and k['T_subcategory'] != 'Temporary Degradation'])

Also, is the username really supposed to be user+001 and not user&001 or user 001?


I think, you can use requests library as follows:

import requestsimport jsonpayload = {'field':'T_Subcategory','operator':'neq','value':'Temporary Degradation'}url = requests.post("http://10.61.202.98:8081/T/a/api/rows/cat/ect/tickets",data=json.dumps(payload))