Home > Software engineering >  Post data (dictionary list) with python requests
Post data (dictionary list) with python requests

Time:11-16

I'd like to post my dictionary list below via python's http requests.

my_data=[
   {
     'kl':'ngt',
     'schemas':
       [
        {
         'date':'14-12-2022',
         'name':'kolo'
        }
      ],
   },

   {

    'kl':'mlk',
    'schemas':
       [
         {
           'date':'23-10-2022',
           'name':'maka'
       }
    ]
  }
 ]

trying to do

url='http://myapi.com/product
x=requests.post(url,json=my_data)

after execution it does not post the products on the database

CodePudding user response:

I think when you want to send json payload in post request you should add headers argument:

headers = {'Content-Type': 'application/json', 'Accept':'application/json'}

r = requests.post(url = 'http://myapi.com/product', data = my_data, headers=headers)

response_result = r.text

then check response status code of the post request, If it isn't 200 then post request doesn't completed successfully.

  • Related