PUT Python 请求未更新数据
PUT Python requests is not updating data
编辑更新:
我尝试了此页面上给出的解决方案,但这对我也不起作用:
我正在使用 python 2.7。我正在尝试使用 PUT 请求。数据必须作为表单数据发送。
import requests
import json
url = "http://httpbin.org/put"
data = {'lastName':'testNew'}
headers = {
'Authorization': "JWTeyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6MTc0LCJlbWFpbCI6ImNlbGVzdGlhbHRlc3QxQGdtYWlsLmNvbSIsInJhbmRvbV9qd3QiOiJlNXg1Q0oifQ.xc5jVAS6ZtTPrjg0LizznT0-sE9W_FkSW5s",
}
response = requests.request("PUT", url,data=data, headers=headers)
print(response.text)
这个请求给我以下响应:
{
"args": {},
"data": "",
"files": {},
"form": {
"lastName": "testNew"
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Authorization": "JWT eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6MTc0LCJlbWFpbCI6ImNlbGVzdGlhbHRlc3QxQGdtYWlsLmNvbSIsInJhbmRvbV9qd3QiOiJlNXg1Q0oifQ.xc5jVAS6ZtTPrjg0LizznT0-sE9W_",
"Connection": "close",
"Content-Length": "16",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.18.4"
},
"json": null,
"origin": "111.93.35.2",
"url": "http://httpbin.org/put"
}
但是当我把 api url 放在我想使用这个请求的地方时,api 挂起并给出了错误的连接错误。
我尝试更新值的 API 与 Postman 一起工作正常,他们在剪贴板中提供的代码如下,它就像一个魅力更新数据一样工作:
import requests
url = "http://myUrl/dashboard/editProfile"
payload = "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"lastName\"\r\n\r\nChangeLastFromCode\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--"
headers = {
'content-type': "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
'Authorization': "JWT eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6MTc0LCJlbWFpbCI6ImNlbGVzdGlhbHRlc3QxQGdtYWlsLmNvbSIsInJhbmRvbV9qd3QiOiJlNXg1Q0oifQ.xc5jVAS6ZtTPrjg0LizznT0-sE9W_",
}
response = requests.request("PUT", url, data=payload, headers=headers)
print(response.text)
如何在不使用上述 Postman 示例中给出的边界值的情况下编写查询?这只是表单数据中的一个简单 PUT 请求。值可以是一个或多个。在上面的示例中,我只使用了 'lastName',我想通过 PUT>
更改它
处理边界条件的最佳方式是多部分编码器。
https://toolbelt.readthedocs.io/en/latest/uploading-data.html
你可以试试下面的代码。
import requests
from requests_toolbelt.multipart.encoder import MultipartEncoder
m = MultipartEncoder(
fields={'field0': 'value', 'field1': 'value',
'field2': ('filename', open('file.py', 'rb'), 'text/plain')}
)
r = requests.post('http://httpbin.org/post', data=m,
headers={'Content-Type': m.content_type})
编辑更新:
我尝试了此页面上给出的解决方案,但这对我也不起作用:
我正在使用 python 2.7。我正在尝试使用 PUT 请求。数据必须作为表单数据发送。
import requests
import json
url = "http://httpbin.org/put"
data = {'lastName':'testNew'}
headers = {
'Authorization': "JWTeyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6MTc0LCJlbWFpbCI6ImNlbGVzdGlhbHRlc3QxQGdtYWlsLmNvbSIsInJhbmRvbV9qd3QiOiJlNXg1Q0oifQ.xc5jVAS6ZtTPrjg0LizznT0-sE9W_FkSW5s",
}
response = requests.request("PUT", url,data=data, headers=headers)
print(response.text)
这个请求给我以下响应:
{
"args": {},
"data": "",
"files": {},
"form": {
"lastName": "testNew"
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Authorization": "JWT eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6MTc0LCJlbWFpbCI6ImNlbGVzdGlhbHRlc3QxQGdtYWlsLmNvbSIsInJhbmRvbV9qd3QiOiJlNXg1Q0oifQ.xc5jVAS6ZtTPrjg0LizznT0-sE9W_",
"Connection": "close",
"Content-Length": "16",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.18.4"
},
"json": null,
"origin": "111.93.35.2",
"url": "http://httpbin.org/put"
}
但是当我把 api url 放在我想使用这个请求的地方时,api 挂起并给出了错误的连接错误。
我尝试更新值的 API 与 Postman 一起工作正常,他们在剪贴板中提供的代码如下,它就像一个魅力更新数据一样工作:
import requests
url = "http://myUrl/dashboard/editProfile"
payload = "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"lastName\"\r\n\r\nChangeLastFromCode\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--"
headers = {
'content-type': "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
'Authorization': "JWT eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6MTc0LCJlbWFpbCI6ImNlbGVzdGlhbHRlc3QxQGdtYWlsLmNvbSIsInJhbmRvbV9qd3QiOiJlNXg1Q0oifQ.xc5jVAS6ZtTPrjg0LizznT0-sE9W_",
}
response = requests.request("PUT", url, data=payload, headers=headers)
print(response.text)
如何在不使用上述 Postman 示例中给出的边界值的情况下编写查询?这只是表单数据中的一个简单 PUT 请求。值可以是一个或多个。在上面的示例中,我只使用了 'lastName',我想通过 PUT>
更改它处理边界条件的最佳方式是多部分编码器。 https://toolbelt.readthedocs.io/en/latest/uploading-data.html
你可以试试下面的代码。
import requests
from requests_toolbelt.multipart.encoder import MultipartEncoder
m = MultipartEncoder(
fields={'field0': 'value', 'field1': 'value',
'field2': ('filename', open('file.py', 'rb'), 'text/plain')}
)
r = requests.post('http://httpbin.org/post', data=m,
headers={'Content-Type': m.content_type})