Python - How to make REST POST request
Question: I have implemented a REST service with Java:

    
    
    @POST
    @Path("/policyinjection")
    @Produces(MediaType.APPLICATION_JSON) 
    public String policyInjection(String request) {
    
        String queryresult = null;
        String response = null;
    
        if (request == null) {
            logger.warning("empty request");
        } else {
    
            //call query() to query redisDB with the request
            queryresult = query(request);
    
            // call inject() to inject returned policy to floodlight
            response = inject(queryresult);
    
        }
        return response;
    }
    

But I need to use Python to implement the client to make a POST request to the
above service. I am new to python, I want to implement a method do it like
this:

    
    
    def callpolicyInjection():
    


Answer: I'm not sure this is the answer to your _exact_ question, but you should take
a look at the [`requests`](http://docs.python-requests.org/en/latest/) module.

It allows to send http POST requests (as well as (m)any other http methods)
mostly in one line:

    
    
    >>> import requests
    
    >>> keys = {'username':'Sylvain', 'key':'6846135168464431', 'cmd':'do_something'}
    >>> r = requests.post("http://httpbin.org/post",params=keys)
    

* * *

Now, the answer is in the `r` objects:

    
    
    >>> print(r)
    <Response [200]>
    
    >>> print(r.text)
    {
      "origin": "70.57.167.19",
      "files": {},
      "form": {},
      "url": "http://httpbin.org/post?cmd=do_something&key=6846135168464431&username=Sylvain",
      "args": {
        "username": "Sylvain",
        "cmd": "do_something",
        "key": "6846135168464431"
      },
      "headers": {
        "Content-Length": "0",
        "Accept-Encoding": "identity, gzip, deflate, compress",
        "Connection": "close",
        "Accept": "*/*",
        "User-Agent": "python-requests/1.2.3 CPython/3.3.1 Linux/3.2.0-0.bpo.4-amd64",
        "Host": "httpbin.org"
      },
      "json": null,
      "data": ""
    }
    

... those are only examples. See requests [response
content](http://www.python-requests.org/en/latest/user/quickstart/#response-
content) documentation for details.

