0

Before downvoting/marking as duplicate, please note:

I have already tried out this, this, this, this,this, this - basically almost all the methods I could find pointed out by the Requests documentation but do not seem to find any solution.

Problem:

I want to make a POST request with a set of headers and form data. There are no files to be uploaded. As per the request body in Postman, we set the parameters by selecting 'form-data' under the 'Body' section for the request. Here is the code I have:

headers = {'authorization': token_string,
           'content-type':'multipart/form-data; boundary=----WebKitFormBoundaryxxxxxXXXXX12345'} # I get 'unsupported application/x-www-form-url-encoded' error if I remove this line

body = {
    'foo1':'bar1',
    'foo2':'bar2',
    #... and other form data, NO FILE UPLOADED  
    }
#I have also tried the below approach
payload = dict()
payload['foo1']='bar1'
payload['foo2']='bar2'
page = ''
page = requests.post(url, proxies=proxies, headers=headers, 
json=body, files=json.dump(body)) # also tried data=body,data=payload,files={} when giving data values

Error

{"errorCode":404,"message":"Required String parameter 'foo1' is not 
present"}

EDIT: Adding a trace of the network console. I am defining it in the same way in the payload as mentioned on the request payload.

Network from dev-tools, I am defining it in the same way in the payload as mentioned

Community
  • 1
  • 1
user2281204
  • 81
  • 1
  • 3
  • 13
  • What happens when you don't pass the `content-type` header at all? Just pass the `authorization` in the header. Remove the `files=json.dump(body)` in your request and replace `json=body` with `data=body` – Pratik Mandrekar Aug 08 '17 at 12:58
  • Could you share the all requests information from Chrome networking tab? – Attila Kis Aug 08 '17 at 13:00
  • @PratikMandrekar I get 'unsupported application/x-www-form-url-encoded' error if I remove `content-type`. `data=body` was the initial thing I tried, didn't work :( – user2281204 Aug 09 '17 at 01:11
  • @AttilaKis this is a backend service to which I'm sending the request. Can you tell me how to get the info you need on Postman? – user2281204 Aug 09 '17 at 01:13

1 Answers1

0

There isn't any gui at all? You could get the network data from chrome, although: Try this:

headers = {'authorization': token_string} 

Probably there is more authorization? Or smthng else?

You shouldn't add Content-Type as requests will handle it for you.

Important, you could see the content type as WebKitFormBoundary, so for the payload you must take, the data from the "name" variable.

Example: enter image description here (I know you won't upload any file, it just an example) - So in this case, for my payload would look like this: payload = {'photo':'myphoto'} (yea there would be an open file etc etc, but I try to keep it simple)

So your payload would be this-> (So always use name from the WebKit)

payload = {'foo1':'foo1data',
           'foo2':'foo2data'}


session.post(url,data = payload, proxies etc...)

Important! As I can see you use the method from requests library. Firstly you always should create a session like this

session = requests.session() -> it will handle cookies, headers, etc, and won't open a new session, or plain requests with every requests.get/post.

Attila Kis
  • 521
  • 2
  • 13
  • added the network data. Even with session.post, without the `content-type` I get `{"errorCode":404,"message":"Content type 'application/x-www-form-urlencoded' not supported"}` – user2281204 Aug 10 '17 at 08:43