[HTTP API] Python request to create new database

I'm using python 3 to perform some post requests.
When I'm trying to create a new database, I always get a '{"message":"No name specified for database","code":"InvalidDatabaseName"}' error.


import requests

multipart_form_data = {"dbname": "testDb"}

headers = {'Content-Type':'multipart/form-data'}

response = requests.post('http://localhost:5820/admin/databases',
                         data=multipart_form_data, headers=headers)



First off, I should point out pystardog, which abstracts all of the massaging you need to do with requests to get this call to work.

If you cannot for some reason import another module, you can make the following tweaks:

A) Do not pass in a Content-Type header. requests will set this when sending the multipart/form-data
B) Use files instead of data to make it a multipart/form-data request
C) The form data needs to be named "root" and at least have values for options and files

>>> headers
{'Authorization': 'Basic YWRtaW46YWRtaW4='}
>>> form_data = fd
>>> headers
{'Authorization': 'Basic YWRtaW46YWRtaW4='}
>>> form_data
{'dbname': 'testdb', 'options': {}, 'files': []}
>>> response = requests.post('http://localhost:5820/admin/databases', files={'root': (None, str(form_data))}, headers=headers)
>>> response.content
b'{"message":"Successfully created database \'testdb\'.\\n"}'
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.