Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

POST & PUT method request confusions

I tried to build my own flask app, On that i faced some problems in PUT & POST requests, I gave my POST & PUT data in x-www-form-urlencoded in postman it works fine but when i go with raw data in postman below error came.

My code goes here-

class OrganisationList(Resource):

def __init__(self):
    self.reqparse = reqparse.RequestParser()
    self.reqparse.add_argument( 'org_name', required=True, help='No org name provided', location=['form', 'json'])
    self.reqparse.add_argument( 'org_phone1', required=True, help='No phone number provided', location=['form', 'json'])
    self.reqparse.add_argument( 'org_phone2', required=False, nullable=True, location=['form', 'json'], default=0)
    self.reqparse.add_argument( 'org_email', required=True, help='No email id provided', location=['form', 'json'])
    self.reqparse.add_argument( 'org_passkey', required=True, help='No org passkey provided', location=['form', 'json'])
    super(OrganisationList, self).__init__()

def get(self):
    organisations = [marshal(organisation, organisation_fields)
                    for organisation in models.Organisation.select()]
    return {'organisations': organisations}

def post(self):
    args = self.reqparse.parse_args()
    try:
        models.Organisation.create(**args)
    except models.IntegrityError:
        return 'some of the fields already exists'
    else:
        return 'saved successfully'

My JSON data: { "org_name": "ABC" "org_phone1": "1234567890" "org_email": "abc@xyz.com" "org_passkey": "admin1234" }

Error: { "message": { "org_name": "No org name provided" } }

But i gave my org_name in JSON. Help me to solve this issue. Thanks in advance.

3 Answers

Hi

I am not taking this course yet , but I can take a look at your JSON and spot a problem

 { "org_name": "ABC" "org_phone1": "1234567890" "org_email": "abc@xyz.com" "org_passkey": "admin1234" }


normally a Json is comma separated as in:

 { "org_name": "ABC", "org_phone1": "1234567890", "org_email": "abc@xyz.com", "org_passkey": "admin1234" }

if this points you in the right direction, plase mark question as answered.

Hi Mark Mneimneh thanks for your answer

In my actual JSON i made the request as a comma separated one but i failed to mark it here, Sorry fot that.

I found some solutions to make that work. But i'm not sure that is a correct way or may be some easy tricks other than this

I put my solution here-

def post(self):
    json_data = request.get_json(force=True)
    args = self.reqparse.parse_args()
    try:
        models.Organisation.create(**args)
    except models.IntegrityError:
        return 'some of the fields already exists'
    else:
        return jsonify('saved successfully')

On the POST method if i gave "json_data = request.get_json(force=True)" it works

Is that the only way or any easy ways are there.

Hi

I do the same thing in Java Spring Framework ... So I would say tour solution is perfectly valid.

Thank you.