IBM Support

How to use Python requests library for uploading file using Watson Studio Local 1.2.3.1 filemgmt API

Question & Answer


Question

When we use following code to upload file using filemgmt API, "Internal Server Error" occurred.
How to fix the code to allow uploading file using filemgmt API?
import requests
import json
url = 'https://wsl-server.company.org/v1/preauth/validateAuth'
r = requests.get(url,auth=('dsxuser02', 'Pass1234'), verify=False)
res = r.json()
bearerToken=res["accessToken"]

header = {'Content-Type': 'multipart/form-data', 'Authorization': 'Bearer {}'.format(bearerToken)}
url = 'https://wsl-server.company.org/api/v1/filemgmt/user-home'

files = {'file': ('aaa.csv', open('aaa.csv', 'rb'),'text/csv')}

r = requests.post(url, headers=header, files=files, verify=False)
r.text

Answer

This problem is caused by 'Content-Type': 'multipart/form-data'setting on the header.
The problem can be solved by deleting this setting, as in this example:
import requests
import json
url = 'https://wsl-server.company.org/v1/preauth/validateAuth'
r = requests.get(url,auth=('dsxuser02', 'Pass1234'), verify=False)
res = r.json()
bearerToken=res["accessToken"]

header = {'Authorization': 'Bearer {}'.format(bearerToken)}
url = 'https://wsl-server.company.org/api/v1/filemgmt/user-home'

files = {'file': ('aaa.csv', open('aaa.csv', 'rb'),'text/csv')}

r = requests.post(url, headers=header, files=files, verify=False)
r.text
Which give following result:
# python filemgmt_upload.py
{"success":true,"result":"aaa.csv (16 Bytes) uploaded\n"}

[{"Business Unit":{"code":"BU059","label":"IBM Software w\/o TPS"},"Product":{"code":"SSHGWL","label":"IBM Watson Studio Local"},"Component":"WSL 1.2.3.1 API;WSL Jupyter Notebook","Platform":[{"code":"PF016","label":"Linux"}],"Version":"WSL 1.2.3.1","Edition":"*All*","Line of Business":{"code":"LOB10","label":"Data and AI"}}]

Document Information

Modified date:
30 December 2019

UID

ibm11095976