Hi all,
I’m working on a Python API call to upload records from a fancy metadata submission form. I’m using the api/0.1/records post method, but I can’t seem to get it to work. It always gives me a 403 error, which if I understand the Swagger UI doc correctly, indicates I don’t have sufficient access. But this is a fresh GeoNetwork server, I’ve opened a session with the default admin username and password, and I can do an empty GET and receive a HTTP 200 response (and changing login details causes that to change to a 401 error).
I’ve tried adding auth details into the post command, but that didn’t change anything.
Any thoughts or ideas?
Code below
-- coding: utf-8 --
“”"
Accesses the GeoNetwork REST API to upload an xml metadata record.
Uses the Requests package for convenience
Made in Spyder Editor
Kim Mortimer, June 2019
“”"
import requests
from xml.dom import minidom
def gn_api_xml_push(XMLFilePath):
GN_BaseURL = ‘http://[! YOUR GEONETWORK HERE]/geonetwork/’ #removed local IP address
GN_API_URL = GN_BaseURL + ‘api/0.1/records’
session = requests.Session()
session.auth=(‘admin’, ‘admin’) #Your username/password here
loginResponse = session.get(GN_API_URL)
OpenedFile = open(XMLFilePath, ‘r’)
XMLDoc = minidom.parse(OpenedFile) #turns a file object into an XML document
OpenedFile.close()
parameters = {
‘file’: [XMLDoc], #Unsure if this is the Array format needed, but the error is the same even if I pass an empty array in
‘rejectIfInvalid’: True
}
requestResponse = session.post(GN_API_URL, data=parameters)
if requestResponse.status_code == 201:
responseDict = requestResponse.json()
if responseDict[‘numberOfRecordsNotFound’] > 0:
return {‘response’: “not found”}
elif responseDict[‘numberOfNullRecords’] > 0:
return {‘response’: “empty”}
elif responseDict[‘numberofRecordsWithErrors’] > 0:
errorString = ‘’
for error in responseDict[‘errors’]:
errorString = errorString + error[‘message’] + ‘\n’
metadataErrorString = ‘’
for value in responseDict[‘metadataErrors’].values():
metadataErrorString = metadataErrorString + value[0][‘message’] + ‘\n’
return {‘response’:“Errors in metadata record”,
‘errors’: errorString,
‘metadataErrors’: metadataErrorString
}
elif responseDict[‘numberOfRecordsProcessed’] > 0:
return {‘response’: ‘processed’}
else:
return {‘response’: ‘Unknown response - HTTP 201 success’}
elif requestResponse.status_code == 403:
return {‘response’: ‘HTTP 403 - insufficient access’,
‘loginResponse’: loginResponse}
else:
return {‘response’: ‘Unknown’,
‘statuscode’: requestResponse.status_code,
‘loginResponse’: loginResponse}
PS: The Swagger UI “try it out” feature for this request also doesn’t seem to work… I just used a known working XML document but the response is 400 -
{
"message": "MultipartException",
"code": "unsatisfied_request_parameter",
"description": "The current request is not a multipart request"
}
Feels weird to me, but might just mean that the Swagger UI is out of date?
Thanks in advance,
Kim
Kim Mortimer
|
Data Manager
|
MERIDIAN - Marine Environmental Research Infrastructure for Data Integration and Application Network
|
Institute for Big Data Analytics, Faculty of Computer Sciences, Dalhousie University
|
p: + 1 902 494 1812 m: +1 902 880 1863
|
a: 6050 University Ave, Halifax, NS, B3H 4R2, Canada
|
w: https://meridian.cs.dal.ca e: k.mortimer@anonymised.com
|
GeoNetwork-devel mailing list
GeoNetwork-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/geonetwork-devel
GeoNetwork OpenSource is maintained at http://sourceforge.net/projects/geonetwork