K
K
Kirill Kudryavtsev2018-04-28 11:05:40
Python
Kirill Kudryavtsev, 2018-04-28 11:05:40

Sending form-data via Requests?

Hello,
There is an API (closed) and you need to make a PATCH request with form-data, how can this be done as painlessly as possible?
PS Google didn't help
PSS Tried using data
as well And like this (generated via postman)
def updateNode(self, id, name, data):
payload = "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"" + str(name) + "\"\r\n\r\n" + str(data) + "\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--"
# send request
req = requests .patch('http://' + config['server']['api'] + '/api/nodes/' + id, data=payload, auth=(self.mail, md5(self.psw)) )
gen_log.info(req.text)
if req.status_code == 200:
else:
return "", 0

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Kirill Kudryavtsev, 2018-04-28
@Deissh

While he himself added to this form, it seems to work.

payload = '------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name="properties"\r\n\r\n' + str(data).replace('\'', '"') + '\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--'
headers = {
    'content-type': "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
    'Content-Type': "application/x-www-form-urlencoded",
    'Cache-Control': "no-cache"
}

req = requests.patch('http://' + config['server']['api'] + '/api/nodes/' + id, data=payload, headers=headers, auth=(self.mail, md5(self.psw)))

If there is a better solution, I will change it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question