Skip to main content

import os

import requests

import json

import cv2

 

directory = r'C:\\boatlaunch'

for filename in os.listdir(directory):

 if filename.endswith(".jpg") or filename.endswith(".jpge") or filename.endswith(".JPG"):

  print(os.path.join(directory, filename))

  filenamepath=os.path.join(directory, filename)

  url = "https://<servername>/fmerest/v3/resources/connections/FME_SHAREDRESOURCE_DATA/filesys/EnvironmentAffair?createDirectories=false&overwrite=false"

  head={

   'Authorization': 'fmetoken token=<mytoken>',

   'Content-Disposition': 'attachment; filename="test.jpg"',

   'Content-Type': 'application/octect-stream',

   'Accept': 'application/json'

   }

  print(head)

  filedata = open(filenamepath,'rb')

  print(filedata)

   

  res = requests.post(url,data=filedata,headers=head)

  print(res.status_code)

  print(res.text)

  print(res.request.headers)

HTTP error 415 is about unsupported media types. What exactly are you trying to do?


Below code works. I was trying to upload files from the local windows folder to FME server resource folder using FME REST API. I was having issue with header of the web api call. I got the solution.

 

import os

import requests

import json

 

apitoken = '<apitoken>'

fmeservername = '<yoursever>'

resourcefoldername = '<yourfoldername>'

 

directory = '<directorypathwherefilesstored>'

for filename in os.listdir(directory):

 if filename.endswith(".jpg") or filename.endswith(".jpge") or filename.endswith(".JPG"):

  print(os.path.join(directory, filename))

  filenamepath=os.path.join(directory, filename)

  url = "https://{0}/fmerest/v3/resources/connections/FME_SHAREDRESOURCE_DATA/filesys/{1}?createDirectories=false&overwrite=true".format(fmeservername,resourcefoldername)

  head={

   'Authorization': 'fmetoken token={0}'.format(apitoken),

   }

  myfile = {'file': open(filenamepath ,'rb')}

  res = requests.post(url, files = myfile, headers=head)   

  print(res.status_code)

  #print(res.text)

  #print(res.request.headers)

 


Reply