如何在 Python 上通过 post 或 get 方法使用 API QR Monkey

发布于 2025-01-16 22:23:35 字数 1412 浏览 1 评论 0原文

我正在尝试使用免费 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)

I'm trying to use the free api https://www.qrcode-monkey.com and I can't find anywhere a valid example for python, I think I followed thru the documentation. I'm doing some trial and on POST I continue getting method errors and on GET I get a lot of 400 errors...

Here is the code with both, anyone knows what I'm doing wrong? Thank you!

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嘿看小鸭子会跑 2025-01-23 22:23:35

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

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

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


还有其他问题。

POST 给出带有圆形QR,但 GET 给出普通正方形

GET 需要将 config 转换为 json 才能获取 circles

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

(但不需要 urlencode< /code>)


完整代码。

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

They didn't show it in documentation but it needs different URL - with api. instead of www.

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

I used DevTools in Firefox/Chrome (tab:Network) to see url used when page generates QR.


There is other problem.

POST gives QR with circles but GET gives with normal squares.

GET needs to convert config to json to get circles

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

(but it doesn't need urlencode)


Full code.

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)

Result:

--- 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文