Vikas Nair
Vikas Nair

Reputation: 101

Flask form POST request not able to handle data

I am trying to execute a form post request for a user management system I have created. I have sessions enable on a flask backend server.

The request is a profile update of a user, I have two conditions this can be executed

1- When the user is logged in

2- User logged out.

When the user is logged in I am able to successfully make an update request.

But when the user is logged out the API is not consistent. As I am sending 10 text parameters with a file object

Case1- When I send text <6 parameters to the server it shows the user has logged out please login again.

Case 2- When I try to send all the 10 parameters with the file object then I get a message on postman saying "Could not get any message from the server"

When sessions data are popped(user logged out) Is the request not able to handle the data? as with fewer parameters, I at least can get the error message which I have configured with my backend flask.

Below is the code I am using to make a request to the server. Both in case of when the user is logged in or logged out

import requests

url = "url/profileupdates"

payload = {'username': 'testingprofile',
'password': 'rhythmtesting_ch',
'email': '[email protected]',
'user_mobile': 'change',
'user_health': 'testing',
'age': 'testing',
'emergency_contact': 'emergency'}
files = [
  ('file', open('/Users/vikasnair/Downloads/E8E4FCC5-9BB7-4530-864B-B662E9884636.jpeg','rb'))
]
headers = {
  'Cookie': 'session=.eJxdzsEKwjAMBuB3yVm8znryTUps4xZoUmk6pIjvbrddyv5b8v2BfMHIjLN6EuQEd4irPGJr15AFLoNSmUlD8yFrxVB7c9ozlCQ_OVEXt2eQN5p9condMArrQKtR8ThvZzd33i-EqS6dXqx0QkWh419p2-zg9wcu90hp.Xqe8zw.z8706Z3t8OUhANpAzcanQyQfBnI'
}

response = requests.request("POST", url, headers=headers, data = payload, files = files)

print(response.text.encode('utf8'))

The below code works when I am trying to send less parameters

import requests

url = "url/profileupdates"

payload = {'username': 'testingprofile',
'password': 'rhythmtesting_ch',
'email': '[email protected]',
'user_mobile': 'change'}
files = [

]
headers = {
  'Cookie': 'session=.eJxdzsEKwjAMBuB3yVm8znryTUps4xZoUmk6pIjvbrddyv5b8v2BfMHIjLN6EuQEd4irPGJr15AFLoNSmUlD8yFrxVB7c9ozlCQ_OVEXt2eQN5p9condMArrQKtR8ThvZzd33i-EqS6dXqx0QkWh419p2-zg9wcu90hp.Xqe8zw.z8706Z3t8OUhANpAzcanQyQfBnI'
}

response = requests.request("POST", url, headers=headers, data = payload, files = files)

print(response.text.encode('utf8'))

Upvotes: 0

Views: 663

Answers (2)

Saud Sarnaik
Saud Sarnaik

Reputation: 1

Try sending files with less parameters. I have faced same problem when sending files and data together and it does not work.

Upvotes: 0

Amitha Mohan
Amitha Mohan

Reputation: 81

Try using

headers = { 'Content-Type': 'multipart/form-data' }

Upvotes: 0

Related Questions