Create a record with attachments in KINTONE using the Python requests module

flow

  1. Make a container with KINTONE
  2. Send attachments in Python --Get fileKey --Merge to record
  3. I'm happy

KITONE container

Create a container for attachments with the field code "Attch".

Send attachments in Python

To attach a file to a record with KINTONE

--Upload file --Join with record

Follow the procedure.

If you write it in Python ↓

#!/usr/bin/python
# coding:utf-8

import requests
import json

URL = "https://xxxxx.cybozu.com:443" #kintone subdomain(xxx.cybozu.com)
APP_ID = "ID" #kintone app ID
API_TOKEN = "APP TOKEN" #kintone API token
KNT_PASS = "xxxxxx" #Login name:Base64 encode password

#KINTONE app field code
#Attachment Attach


class KINTONE:
    def UploadToKintone(self, url, knt_pass,path,filename):
        img = open(path + filename, 'rb')
        files={'file':(filename,img,'image/jpeg')}

        headers = {"X-Cybozu-Authorization": knt_pass , 'X-Requested-With': 'XMLHttpRequest'} 
        resp=requests.post(url+"/k/v1/file.json",files=files,headers=headers)

        return resp 
    
    def PostToKintone(self,url,appId,apiToken,filekey):
        record = {
            "Attch":{'type':"FILE","value" :[{'fileKey':filekey}]}
            #If you want to insert data in other fields','Separated by
        }
        data = {'app':appId,'record':record}
        headers = {"X-Cybozu-API-Token": apiToken, "Content-Type" : "application/json"}
        resp=requests.post(url+'/k/v1/record.json',json=data,headers=headers)
        
        return resp

if __name__ == '__main__':
    Path='/tmp/' #File save path
    FileName='image.jpg' #file name
    
    knt=KINTONE()
    resp=knt.UploadToKintone(URL, KNT_PASS,Path,FileName)

    txt=json.loads(resp.text)
    FileKey=txt['fileKey']
    resp=knt.PostToKintone(URL, APP_ID, API_TOKEN,FileKey)
    print resp.text

The output is

{"id":"1","revision":"1"}

I felt happy next to me.

Recommended Posts

Create a record with attachments in KINTONE using the Python requests module
Write data to KINTONE using the Python requests module
Create a graph using the Sympy module
Create a compatibility judgment program with the random module of python.
Create a GIF file using Pillow in Python
Create a Python module
Create a virtual environment with conda in Python
Create a new page in confluence with Python
A story that didn't work when I tried to log in with the Python requests module
Create a MIDI file in Python using pretty_midi
Create a data collection bot in Python using Selenium
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 1 ~
[Python] Create a ValueObject with a complete constructor using dataclasses
Register a ticket with redmine API using python requests
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 2 ~
Create a fake Minecraft server in Python with Quarry
Create a company name extractor with python using JCLdic
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 3 ~
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 4 ~
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 5 ~
Create a function in Python
Create a dictionary in Python
Create a directory with python
Create a local scope in Python without polluting the namespace
Create a list in Python with all followers on twitter
Create a child account for connect with Stripe in Python
Let's create a script that registers with Ideone.com in Python.
Probably the easiest way to create a pdf with Python3
Create a Twitter BOT with the GoogleAppEngine SDK for Python
Solve the Japanese problem when using the CSV module in Python.
Create a python GUI using tkinter
Get the weather with Python requests
Create a virtual environment with Python!
Get the weather with Python requests 2
Create a binary file in Python
Try using the Python Cmd module
Create a random string in Python
Master the weakref module in Python
Control the motor with a motor driver using python on Raspberry Pi 3!
Create a color picker for the color wheel with Python + Qt (PySide)
How to create a heatmap with an arbitrary domain in Python
[Ev3dev] Create a program that captures the LCD (screen) using python
Read a file in Python with a relative path from the program
Solve the subset sum problem with a full search in Python
The story of making a module that skips mail with python
Create an application that just searches using the Google Custom Search API with Python 3.3.1 in Bottle
[Python] Create a program to delete line breaks in the clipboard + Register as a shortcut with windows
Spiral book in Python! Python with a spiral book! (Chapter 14 ~)
Create a Python function decorator with Class
Try using the Wunderlist API in Python
Build a blockchain with Python ① Create a class
Try using the Kraken API in Python
Behind the flyer: Using Docker with Python
Create a dummy image with Python + PIL.
Write the test in a python docstring
Create a simple GUI app in Python
[Python] Create a virtual environment with Anaconda
Let's create a free group with Python
Display Python 3 in the browser with MAMP
Create a JSON object mapper in Python
Search the maze with the python A * algorithm