Home > Software design >  Response 400 Bad Request while posting a list
Response 400 Bad Request while posting a list

Time:08-24

I am trying to POST a list to the DRF server and I'm getting 400 bad request response (The URL and token are correct, the server is live)

Here is my code:

import requests
import json

x = [2423, 2342, 443]
y = [532, 5432, 543]
w = [7456, 256, 564]
h = [2345, 6543, 874]

Boxes = []

for i in range(len(x)):
    Boxes.append({'X': x[i], 'Y': y[i], 'W': w[i], 'H': h[i]})

res = json.dumps(Boxes)


r = requests.post(
    "http://art1x.pythonanywhere.com/snippets/",
    data=json.dumps({"X": res,
                     "Y": '0',
                     "W": '0',
                     "H": '0',
                     "Amount": '0'
                     }),
    headers={'Authorization': 'Token 353101b2657b3779199777984c131f33b78656be',
             "Content-Type": "application/json"},
)
print(r.status_code)

I am converting Boxes to a JSON object and then posting it.

Any help would be appreciated, thank you!

CodePudding user response:

Do this: print(r.json()) and you will see the error message

{'X': ['Ensure this field has no more than 100 characters.']}

Indeed len(res) has 132 characters. You may need to shorten it

To extend the allowed length limit (reference) you could try:

#Add to model field
class MyModel(models.Model):
    validate_len = models.CharField(max_length=150)
  • Related