如何将 API QR Monkey 与 post 一起使用或在 Python 上使用 get 方法

How to use API QR Monkey with post or get method on Python

我正在尝试使用免费的 api https://www.qrcode-monkey.com,但在任何地方都找不到 python 的有效示例,我想我是按照文档进行操作的。我正在做一些试验,在 POST 上我继续收到方法错误,在 GET 上我收到很多 400 错误。 .

这是两者的代码,有人知道我做错了什么吗?谢谢!

import requests
from urllib.parse import quote, urlencode


class QrManager:
    def __init__(self):
        self.url = "https://qrcode-monkey.com/"

    def get_data_post(self):
        url = self.url + "qr/custom"
        payload = {
            "data": "https://www.google.com",
            "config": {
                "body": "circle",
            },
            "size": 300,
            "download": False,
            "file": "png"
            }
        req = requests.post(url, json=payload)
        return req

    def get_data_get(self):
        template_url = self.url + "qr/custom/?{}"
        params = {
            "data": "https://www.google.com",
            "config": {
                "body": "circle",
            },
            "size": 300,
            "download": False,
            "file": "png"
        }
        url = template_url.format(urlencode(params, safe="()", quote_via=quote))
        req = requests.get(url)
        return req

qrm = QrManager()

# response = dm.get_data_post()
response = qrm.get_data_get()

print(response.status_code)
print(response.url)
print(response.text)

他们没有在文档中显示它,但它需要不同的 URL - 用 api. 而不是 www.

https://api.qrcode-monkey.com/qr/custom

我在Firefox/Chrome(tab:Network)中使用了DevTools来查看页面生成QR时使用的url。


还有其他问题。

POST 给出 QRcirclesGET 给出正常 squares.

GET 需要将 config 转换为 json 才能得到 circles

"config": json.dumps({"body": "circle"})

(但不需要urlencode)


完整代码。

import requests
#from urllib.parse import quote, urlencode
import json

class QrManager:
    def __init__(self):
        self.url = "https://api.qrcode-monkey.com/qr/custom"

    def get_data_post(self):
        # it converts `config` to `json` automatically (because it sends all `payload` as `json`)  

        payload = {
            "data": "https://blog.furas.pl",
            "config": {
                "body": "circle",
            },
            "size": 300,
            "download": False,
            "file": "png"
            }
        
        response = requests.post(self.url, json=payload)

        return response

    def get_data_get(self):
        # it needs to convert `config` to `json` manually

        payload = {
            "data": "https://blog.furas.pl",
            "config": json.dumps({
                "body": "circle"
            }),
            "size": 300,
            "download": False,
            "file": "png"
        }
        
        #payload = urlencode(payload, safe="()", quote_via=quote)
       
        response = requests.get(self.url, params=payload)

        return response

# --- main ---

qrm = QrManager()

print('\n--- GET ---\n')

response = qrm.get_data_get()
print('status:', response.status_code)
print('url:', response.url)
print(response.text[:100])

with open('QR_GET.png', 'wb') as f:
    f.write(response.content)
    
print('\n--- POST ---\n')

response = qrm.get_data_post()
print('status:', response.status_code)
print('url:', response.url)
print(response.text[:100])

with open('QR_POST.png', 'wb') as f:
    f.write(response.content)

结果:

--- GET ---

status: 200
url: https://api.qrcode-monkey.com/qr/custom?data=https%3A%2F%2Fblog.furas.pl&config=%7B%22body%22%3A+%22circle%22%7D&size=300&download=False&file=png
�PNG

IHDR\t�{bKGD��������IDATx��ON[��š�6#�P��hŮ#H� ��[��M��T=3@J


--- POST ---

status: 200
url: https://api.qrcode-monkey.com/qr/custom
�PNG

IHDR\t�{bKGD��������IDATx��ON[��š�6#�P��hŮ#H� ��[��M��T=3@J