0

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

2
  • Multiple things can happen here. Do you have an http response code or a stacktrace to share. There are not enough details with your current question. Commented Nov 15, 2022 at 13:07
  • status code is different from 200, is in these condition used data=my_data instead of json=my_data Commented Nov 15, 2022 at 13:12

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

the code is different from 200 , i will like to know using json=my_data is valid , if it is known that my_data is a dictionary list
@PathiaNanto check the documentation for making requests: requests.readthedocs.io/en/latest/user/quickstart/…
Normally with json= you can pass a dict. Currently you pass a list, it might be a problem (did not double check on my side). What is your http status?
@Oghli I would like to turn my dictionary list into a dictionary like this, all_data={ {...},{...} } ?
@PathiaNanto see this post on stackoverflow: stackoverflow.com/questions/5236296/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.