2

I kept getting <Response [400]> in my terminal while running the script.

I have tried

import requests
import json

url = 'http://172.19.242.32:1234/vse/account'

data = '{ 
  "account_id": 1008,
  "email_address": "bhills_4984@mailinator.com",
  "password": "qqq",
  "account_type": "customer",
  "name_prefix": "",
  "first_name": "Beverly",
  "middle_names": "",
  "last_name": "Hills",
  "name_suffix": "",
  "non_person_name": false,
  "DBA": "",
  "display_name": "BeverlyHills",
  "address1": "4984 Beverly Dr",
  "address2": "4984 Beverly Dr",
  "address3": "",
  "city": "Beverly Hills",
  "state": "CA",
  "postal_code": "90210",
  "nation_code": "90210",
  "phone1": "3105554984",
  "phone2": "",
  "phone3": "",
  "time_zone_offset_from_utc": -5,
  "customer_type": "2",
  "longitude": -118.4104684,
  "latitude": 34.1030032,
  "altitude": 0
}'

headers = {'content-type': 'application/json'}

r = requests.post(url, data=json.dumps(data), headers=headers)

print r

What did I do wrong ?

Iron Fist
  • 10,739
  • 2
  • 18
  • 34
code-8
  • 54,650
  • 106
  • 352
  • 604

1 Answers1

8

Change

r = requests.post(url, data=json.dumps(data), headers=headers)

to

r = requests.post(url, data=data, headers=headers)

because data is not a dict that must be transformed to json but is already json.

Ben Beirut
  • 733
  • 3
  • 12
  • 2
    wasn't it `json=data`? if you pass `data=data` then it will be encoded as `x-www-form-urlencoded` instead of `application/json`. – Kos Aug 31 '16 at 19:07
  • @Kos Ideally, data would be a dictionary and he would pass it using the json parameter instead of data. `requests.post(url, json=data)` – Ben Beirut Aug 31 '16 at 19:11