Sending POST request with payload using a module requests - python

How to send a POST request with this payload (optional: with file)? Should I send all of headers for proper work of it?
This doesn't work:
data = {"to":"6642","send":"1","go":"1","id":"6642"}
f = open("f.jpg","rb")
r = requests.post(url,data=data,files={"f.jpg":f})
parameters and payload
Sorry for my English and thanks for answers!

Check this, I hope you attached the whole payload.
data = {'to':'6642',
'send'='1',
'go':'1',
'id':'6642',
"walltext":"TEST",
"wallsend":"TEST"}
files = {'file2':open("f.jpg","rb")
r = requests.post(url,data=data,files = files)

Related

Pyton requests: Extracting data from payload

this is my first ever question on here.
Currently I am looking into the Python requests module. I am trying to automate a task for which i need to pass on a csrf token.
The csrf token can be found in the payload of a previous request.
How can I extract the value out of the Payload?
Example Payload:
value1: ABCD
value2: EFGH
csrf_token: the token I am looking for
value3: false
Sorry if this is a dumb or easy question but I am not able to solve it right now.
Thanks for your help!
Here are some examples of where the data you might be looking for is located.
import requests
response = requests.get('http://some_url')
# raise an exception if the status code != 200
response.raise_for_status()
# the contents of the response. (bytes)
contents = response.contents
# the contents of the response if the contents are
# formatted as JSON. (dictionary)
contents = response.json()
# the headers of the response. (dictionary)
headers = response.headers
# You also have to consider if you are using the correct HTTP protocol
payload = {}
response = requests.put('http://some_url', json=payload)
response = requests.post('http://some_url', json=payload)
# the responses are going to function just like when using the "get" protocol

How to upload a binary/video file using Python http.client PUT method?

I am communicating with an API using HTTP.client in Python 3.6.2.
In order to upload a file it requires a three stage process.
I have managed to talk successfully using POST methods and the server returns data as I expect.
However, the stage that requires the actual file to be uploaded is a PUT method - and I cannot figure out how to syntax the code to include a pointer to the actual file on my storage - the file is an mp4 video file.
Here is a snippet of the code with my noob annotations :)
#define connection as HTTPS and define URL
uploadstep2 = http.client.HTTPSConnection("grabyo-prod.s3-accelerate.amazonaws.com")
#define headers
headers = {
'accept': "application/json",
'content-type': "application/x-www-form-urlencoded"
}
#define the structure of the request and send it.
#Here it is a PUT request to the unique URL as defined above with the correct file and headers.
uploadstep2.request("PUT", myUniqueUploadUrl, body="C:\Test.mp4", headers=headers)
#get the response from the server
uploadstep2response = uploadstep2.getresponse()
#read the data from the response and put to a usable variable
step2responsedata = uploadstep2response.read()
The response I am getting back at this stage is an
"Error 400 Bad Request - Could not obtain the file information."
I am certain this relates to the body="C:\Test.mp4" section of the code.
Can you please advise how I can correctly reference a file within the PUT method?
Thanks in advance
uploadstep2.request("PUT", myUniqueUploadUrl, body="C:\Test.mp4", headers=headers)
will put the actual string "C:\Test.mp4" in the body of your request, not the content of the file named "C:\Test.mp4" as you expect.
You need to open the file, read it's content then pass it as body. Or to stream it, but AFAIK http.client does not support that, and since your file seems to be a video, it is potentially huge and will use plenty of RAM for no good reason.
My suggestion would be to use requests, which is a way better lib to do this kind of things:
import requests
with open(r'C:\Test.mp4'), 'rb') as finput:
response = requests.put('https://grabyo-prod.s3-accelerate.amazonaws.com/youruploadpath', data=finput)
print(response.json())
I do not know if it is useful for you, but you can try to send a POST request with requests module :
import requests
url = ""
data = {'title':'metadata','timeDuration':120}
mp3_f = open('/path/your_file.mp3', 'rb')
files = {'messageFile': mp3_f}
req = requests.post(url, files=files, json=data)
print (req.status_code)
print (req.content)
Hope it helps .

Attaching file in pipedrive

https://developers.pipedrive.com/docs/api/v1/#!/Files/post_files
Doesn't show request example and I can't send POST request via python.
My error is: "No files provided"
Maybe someone has an example for this request?
My code:
import requests
with open('qwerty.csv', 'rb') as f:
r = requests.post('https://api.pipedrive.com/v1/files',
params={'api_token': 'MY_TOKEN'}, files={'file': f})
Try to decouple the operation.
files = {'file': open('qwerty.csv', 'rb')}
r = requests.post('https://api.pipedrive.com/v1/files',
params={'api_token': 'MY_TOKEN'}, files=files)
Well, I was dumb.
All you need is check request in chrome develop tools and play with it for some time.
import requests
files = {'file': ('FILE_NAME', open('fgsfds.jpg', 'rb'), 'CONTENT_TYPE')}
r = requests.post('https://api.pipedrive.com/v1/files',
params={'api_token': 'TOKEN'},
data={'file_type':'img', 'deal_id':DEAL_ID}, files=files)
Update
Recently used this endpoint (Feb 2021). Turns out the endpoint doesn't accept the 'file_type' parameter anymore.

How to send multidimensional POST in Python

I use requests.api lib to send post request. What I want is to send multidimensional POST data and I always come up short with this code:
import requests
url = 'http://someurl.com';
request_data = {}
request_data['someKey'] = 'someData'
request_data['someKeytwo'] = 'someData2'
request_data['requestData'] = {'someKey3': 'someData3'}
login = requests.post(url, data=login_data)
On the receiving end i get a POST with "requestData" => "someKey3" instead of "requestData" => ["someKey3" => 'someData3']
How do I send the correct POST?
The correct answer for my question is:
import requests
url = 'http://someurl.com';
request_data = {}
request_data['requestData[someKey3]'] = 'someData3'
login = requests.post(url, data=request_data)
Simply use:
import json
login = requests.post(rul, data=json.dumps(login_data))
This way you receive a json on the the receiving side.

Sending json post request with python

With the firefox plugin "HttpFox" i'm getting the POST request which looks like this:
{'json':'{"command":"SEARCH","data":{"someData":"someValue","otherData":"otherData"}}'}
Now i need to send a http request build with python to get the same data as i would get via browser. See code:
headers = {'Content-type': 'application/json; charset=utf-8'}
payload = ?
req = requests.post(url, data=json.dumps(payload), headers = headers)
My problem is:
I'm not sure how to build the payload. It should be a dictionary as well, but im confused because of the POST type which is delivered with HttpFox. There are two dictionaries inside the main dictionary.
How should i handle this ?
Appreciate any help.
Ok, i found the solution:
It was necessary to build a dict like this:
valueString = """{"command":"SEARCH","data":{"someData":"someValue","otherData":"otherData"}}"""
/// the """ ensures that the whole text between """ is handled as a string.
payload = {'json': valueString}
The key 'json' requieres a string. In this case the string looks like a dictionary.
That's it.

Categories