简体   繁体   English

Postman API 请求在 Python

[英]Postman API request in Python

I am trying to web-scrape data from https://www.mcmaster.com .我正在尝试从https://www.mcmaster.com中抓取数据。 They have provided me with a.pfx file and a passphrase.他们为我提供了一个 .pfx 文件和一个密码。 When making a GET request on Postman using their.json file, I input my website login/password and upload the.pfx certificate with its passphrase and everything works fine.当使用他们的.json 文件对 Postman 发出 GET 请求时,我输入了我的网站登录名/密码并上传了带有密码的 .pfx 证书,一切正常。 Now I am trying to do this same thing but in Python, but am a bit unsure.现在我正在尝试做同样的事情,但在 Python 中,但有点不确定。

Here is my current Python code, I am unsure where I would put the website email/password login and how to successfully do a GET request.这是我当前的 Python 代码,我不确定将网站电子邮件/密码登录放在哪里以及如何成功执行 GET 请求。

import requests_pkcs12
from requests_pkcs12 import get

r = get('https://api.mcmaster.com/v1/login', pkcs12_filename='Schallert.pfx', pkcs12_password='mcmasterAPI@1901')

response = requests_pkcs12.get(r)

print(response.text)

Here is how I have it setup in Postman (Website email/pw login)这是我在 Postman 中设置的方法(网站电子邮件/密码登录) 在此处输入图像描述

.PFX Certificate page .PFX 证书页面在此处输入图像描述

Postman has a built in feature where it will convert requests into code. Postman 有一个内置功能,可以将请求转换为代码。 You can do it like so:你可以这样做:

  1. On the far right click the Code Snippet Button (</>)在最右侧单击代码片段按钮 (</>)
  2. Once you are on that page, there is two available python options进入该页面后,有两个可用的 python 选项
  3. Then all you need to do is copy the code into a Python file and add all your customizations (Should be already optimized)然后您需要做的就是将代码复制到 Python 文件中并添加所有自定义项(应该已经优化)

One thing I'll warn you about though is the URL.我要警告你的一件事是 URL。 Postman doesn't add http:// or https:// to the URL, which means Python will throw a No Scheme Supplied error. Postman doesn't add http:// or https:// to the URL, which means Python will throw a No Scheme Supplied error.

Available Packages for Auto Conversion:自动转换的可用软件包:

  1. Requests要求
  2. http.client http.client

Meaning you will have to use a different package instead of requests_pkcs12这意味着您将不得不使用不同的 package 而不是requests_pkcs12

After a quick web search, it looks like you need to create a temporary certificate as a .pem file, which is then passed to the request.在快速搜索 web 之后,您似乎需要创建一个临时证书作为.pem文件,然后将其传递给请求。

from contextlib import contextmanager
from pathlib import Path
from tempfile import NamedTemporaryFile

import requests
from cryptography.hazmat.primitives.serialization import Encoding, PrivateFormat, NoEncryption
from cryptography.hazmat.primitives.serialization.pkcs12 import load_key_and_certificates


@contextmanager
def pfx_to_pem(pfx_path, pfx_password):
    pfx = Path(pfx_path).read_bytes()
    private_key, main_cert, add_certs = load_key_and_certificates(pfx, pfx_password.encode('utf-8'), None)

    with NamedTemporaryFile(suffix='.pem') as t_pem:
        with open(t_pem.name, 'wb') as pem_file:
            pem_file.write(private_key.private_bytes(Encoding.PEM, PrivateFormat.PKCS8, NoEncryption()))
            pem_file.write(main_cert.public_bytes(Encoding.PEM))
            for ca in add_certs:
                pem_file.write(ca.public_bytes(Encoding.PEM))
        yield t_pem.name

with pfx_to_pem('your pfx file path', 'your passphrase') as cert:
    requests.get(url, cert=cert, data=payload)

The package requests_pkcs12 is a wrapper written above the requests package. package requests_pkcs12是写在requests package 之上的包装器。 So, all the parameters that accept requests will accept by the requests_pkcs12 .因此,所有接受requests的参数都将被requests_pkcs12接受。

Here is the source code proof for that.这是源代码证明。 https://github.com/m-click/requests_pkcs12/blob/master/requests_pkcs12.py#L156 https://github.com/m-click/requests_pkcs12/blob/master/requests_pkcs12.py#L156

Also, from your screenshots, I understood that you are using POST not GET .另外,从您的屏幕截图中,我了解到您使用的是POST而不是GET

import json
from requests_pkcs12 import post

url = "https://api.mcmaster.com/v1/login"
payload = {'Username': 'yourusername',
           'Password': 'yourpassword'}

resp = post(url, pkcs12_filename='Schallert.pfx', pkcs12_password='mcmasterAPI@1901', data=json.dumps(payload))
print(resp)

Footnote: I hope the password mcmasterAPI@1901 it's a fake one.脚注:我希望密码mcmasterAPI@1901是假的。 if not please don't share any credentials in the platform.如果不是,请不要在平台上分享任何凭据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Python 在 Postman 之外验证 API 请求 - Authenticating API Request outside of Postman using Python POST请求在Postman中有效,但在Python中无效 - POST request works in Postman but not in Python GET api 请求在 POSTMAN 中有效,但在 python 代码中无效,我想将响应下载为 Z628CB5675FF524F3FEE7AAE91 - GET api request is working in POSTMAN but not in python code , I want to download the response as csv 无法通过使用 python 请求库调用 API 来获取正确的数据,而它与 postman 一起使用 - can't get the correct data by calling an API using python request library while it works with postman 我应该如何设置 postman 来测试 API 的 python request.post 方法 - How should I set up the postman to test API for python request.post method 如何在SHARECHAT APP发帖请求 POSTMAN API - How to post a request in SHARECHAT APP in POSTMAN API GET请求通过Python工作,但不通过Postman工作 - GET request working through Python but not through Postman 将 POST 请求从邮递员导出到 Python - Export POST Request from postman to Python JSON output 适用于 postman 但不适用于 python 后请求 - JSON output works in postman but not in python post request Laravel + Python 请求不起作用,但在 Postman 中它起作用 - Laravel + Python request not working but in Postman it works
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM