Avenger
Avenger

Reputation: 877

Send post request with params in python

I have created a post route using werkzeug.

http://localhost:8000/v1/api/<serial_id>/data

def url_map():
    tenants = [
        Submount(
            "/<serial_id>",
            [
                Rule(
                    "/data",
                    methods=["POST"],
                    endpoint=getData,
                )
            ],
        )
    ]

    api = [Submount("/api", api)]

    rules = [Submount("/v1", api)]
    return Map(rules, strict_slashes=False)


   def getData(self, request, serial_id):
        logger.error('88888888888888888888888888888888888888888888888888888888888')
        logger.error(serial_id)
        return {'status': 'ok'}

I am sending request to the path:

requests.post('http://localhost:8000/v1/api/<serial_id>/data',
                                                data= json.dumps({'data':'data'}),
                                                params={'serial_id':1}
                                                )

The problem is instead of printing 1 it print serial_id as <serial_id>.

Expected is:
88888888888888888888888888888888888888888888888888888888888
1

Actual is:
88888888888888888888888888888888888888888888888888888888888
<serial_id>

Upvotes: 1

Views: 2619

Answers (1)

Dev-I-J
Dev-I-J

Reputation: 49

As @Md Jewele Islam states in the comments the url variable must be like:

url = 'http://localhost:8000/v1/api/{}/data'.format(str(serial_id))

and the request must be sent like:

import json
res = requests.post(url , data= json.dumps({'data':'data'}), params={'serial_id':1})

So you can print the response by:

print(res.text)

Upvotes: 2

Related Questions