Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Post call throwing 400 Bad Request

Tags:

python

rest

curl

I am writing a python script which will call a REST POST endpoint but in response I am getting 400 Bad Request where as if I do same request with curl, it returns me 200 OK. Code snippet for python script is below

import httplib,urllib
def printText(txt):
  lines = txt.split('\n')
  for line in lines:
      print line.strip()

httpServ = httplib.HTTPConnection("127.0.0.1", 9100)
httpServ.connect()

params = urllib.urlencode({"externalId": "801411","name": "RD Core","description": "Tenant create","subscriptionType": "MINIMAL","features":   {"capture":False,"correspondence": True,"vault": False}})

 headers = {"Content-type": "application/json"}
 httpServ.request("POST", "/tenants", params, headers)
 response = httpServ.getresponse()
 print response.status, response.reason
 httpServ.close()

and corresponding curl request is

curl -iX POST \
-H 'Content-Type: application/json' \
-d '
{
    "externalId": "801411",
    "name": "RD Core seed data test",
    "description": "Tenant for Core team  seed data testing",
    "subscriptionType": "MINIMAL",
    "features": {
        "capture": false,
        "correspondence": true,
        "vault": false
    }
}' http://localhost:9100/tenants/

Now I am not able figure out where is the issue in python script.

like image 581
RIPAN Avatar asked Sep 18 '15 18:09

RIPAN


People also ask

How to debug Bad Request 400?

Clear browser cookies. A website may fail to comply with your request due to old or corrupt cookies. As a quick fix, consider clearing your browser cache and cookies. Repeat this exercise from time to time to avoid running into a 400 Bad Request error.

What causes a 400 Bad Request error?

The HTTP status 400 – bad request indicates that the request sent to the server is invalid or corrupted. Just like other 4xx status codes, a 400 bad request is a client-side issue. It can be caused by malformed request syntax, invalid request message framing, or deceptive request routing.

What is 403 Python?

The 403 error pops up when a user tries to access a forbidden page or, in other words, the page they aren't supposed to access. 403 is the HTTP status code that the webserver uses to denote the kind of problem that has occurred on the user or the server end.


2 Answers

Try using requests (install with pip install requests) instead of urllib.

Also, enclose your data as JSON in the request body, don't pass them as URL parameters. You are passing JSON data in your curl example as well.

import requests


data = {
    "externalId": "801411",
    "name": "RD Core",
    "description": "Tenant create",
    "subscriptionType": "MINIMAL",
    "features": {
        "capture": False,
        "correspondence": True,
        "vault": False
    }
}

response = requests.post(
    url="http://localhost:9100/tenants/",
    json=data
)

print response.status_code, response.reason

EDIT

From https://2.python-requests.org/en/master/user/quickstart/#more-complicated-post-requests:

Note, the json parameter is ignored if either data or files is passed.

Using the json parameter in the request will change the Content-Type in the header to application/json.

like image 130
Dušan Maďar Avatar answered Sep 25 '22 19:09

Dušan Maďar


The problem in your code is, you set Content-Type header as application/json and not sending data in json format

 import httplib, json

 httpServ = httplib.HTTPConnection("127.0.0.1", 9100)
 httpServ.connect()
 headers = {"Content-type": "application/json"}
 data = json.dumps({
    "externalId": "801411",
    "name": "RD Core",
    "description": "Tenant create",
    "subscriptionType": "MINIMAL",
    "features": {
        "capture": False,
        "correspondence": True,
        "vault": False
    }
 })
 # here raw data is in json format
 httpServ.request("POST", "/tenants", data, headers)
 response = httpServ.getresponse()
 print response.status, response.reason
 httpServ.close()
like image 26
Renjith Thankachan Avatar answered Sep 25 '22 19:09

Renjith Thankachan