Home > Back-end >  How to use API QR Monkey with post or get method on Python
How to use API QR Monkey with post or get method on Python

Time:03-26

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)

CodePudding user response:

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://blog.furas.pl&config={"body": "circle"}&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

  • Related