Python 从 Postman 请求代码片段

标签 python python-requests postman

我正在尝试检查电子邮件是否已存在于网站中。目前正在使用来自 Postman 的示例 Python 代码。

有效的示例代码:

import requests

url = "https://registration.mercadolivre.com.br/registration/"

payload = "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"signUp.email\"\r\n\r\ntest@hotmail.com\r\n-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"signUp.repEmail\"\r\n\r\ntest@hotmail.com\r\n-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"signUp.newsletter\"\r\n\r\ntrue\r\n-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"source\"\r\n\r\nmercadolibre\r\n-----011000010111000001101001--"
headers = {
'content-type': "multipart/form-data; boundary=---011000010111000001101001",
'cache-control': "no-cache",
'postman-token': "179cabe2-dd22-490e-8fbd-15bf2977feb5"
}

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

print(response.text)

我不确定为什么 Postman 会在该字符串中编码有效负载。当我尝试传递字典时,它不再起作用了

无效的代码:

import requests

url = "https://registration.mercadolivre.com.br/registration/"

payload = { 'signUp.email': 'test@hotmail.com',
 'signUp.repEmail': 'test@hotmail.com',
 'signUp.newsletter': 'true',
 'source': 'mercadolibre' }
headers = {
'content-type': "multipart/form-data; boundary=---011000010111000001101001",
'cache-control': "no-cache",
'postman-token': "22a12fa5-5f68-685c-124d-db0ef6eb334c"
}

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

print(response.text)

我想知道为什么我不能传递 json 或字典。

最佳答案

你的 json 版本工作正常

import requests

url = "https://registration.mercadolivre.com.br/registration/"

payload = { 'signUp.email': 'test@hotmail.com',
 'signUp.repEmail': 'test@hotmail.com',
 'signUp.newsletter': 'true',
 'source': 'mercadolibre' }
headers = {
'content-type': "multipart/form-data; boundary=---011000010111000001101001",
'cache-control': "no-cache",
'postman-token': "22a12fa5-5f68-685c-124d-db0ef6eb334c"
}

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

print(response.text)

Output

> python payload.py
<!DOCTYPE html>
<!--[if lt IE 7]> <html lang="es-AR" class="no-js lt-ie10 lt-ie9 lt-ie8 lt-ie7 ie6"> <![endif]--><!--[if IE 7]> <html lang="es-AR" class="no-js lt-ie10 lt-ie9 lt-ie8 ie7"> <![endif]--><!--[if IE 8]> <html lang="es-AR" class="no-js lt-ie10 lt-ie9 ie8"> <![endif]--><!--[if IE 9]> <html lang="es-AR" class="no-js lt-ie10 ie9"> <![endif]--><!--[if (gt IE 9)|!(IE)]> <!--> <html lang="es-AR" class="no-js"> <!--<![endif]-->
<head><meta content="text/html; charset=UTF-8"/><title>MercadoLivre - Ocorreu um erro</title><link rel=stylesheet href="/css/null/7.0.5/errorPage.css"/><!--[if lt IE 7 ]><script src="http://www.mercadolibre.com/org-img/pcorner/js/dd_belatedPNG.min.js"></script><script> DD_belatedPNG.fix('img, .ico, .png24fix, .ch-expando-trigger'); //fix any <img> or .ico background-images </script><![endif]--><link rel="shortcut icon" href="//http2.mlstatic.com/ui/navigation/1.5.9/mercadolibre/favicon.ico"/></head><body data-country=AR><div class=errorPage500><p class=ups>Ops!</p><div class=errorMessage><h2>Ocorreu um erro</h2><p>Por favor, tente mais tarde.</p></div></div></body></html>
<!--
    Stats
    Generate time       : 36 ms
    Render time         : 1 ms
    Compress time       : 1 ms
    Total time          : 38 ms
    HostName            : i-3647d238-10.17.67.12
-->

I guess problem may be your version mine python version is 3.4.2 Requests is 2.11.1

关于Python 从 Postman 请求代码片段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40489016/

相关文章:

python - Series 的真值不明确 - 调用函数时出错

python - 从 pickle 加载的 HMM 看起来未经训练

mysql - 定期进行网络抓取,以抓取自上次运行以来网站上的新信息

postman - 跨选项卡使用 Postman 变量

python - QTableWidget:删除或扩展到整行单元格的事件突出显示

Python 请求无法解码文本

python - 通过 python 自动化 .get 请求

asp.net-core - 带有 docker-compose 的 Identity Server 4 在/connect/authorize/callback 之后重定向到登录页面

node.js - 使用 Node js 进行 REST 调用 : "Error: connect ETIMEDOUT" and "http code 0"

python - 'TreeDict'(或 TreeMap )在实践中有什么用?