Problems creating dataset via API

Status
Not open for further replies.

onlyanegg

Cadet
Joined
Aug 6, 2015
Messages
7
Hi,
I'm having trouble creating a dataset with the REST API. I'm using the python requests module. The server responds with 301 moved permanently, and then, without my intention, a get request is made by client. This is confusing because the python function returns the status from the get call (which succeeds) rather than the post call. If I make another request with the uri given by the location field of the response, I get 400 bad request. Can anyone help me out with this?

Note that the uri that I specify the first post request is identical to the uri in the location field of the response except for a trailing forward slash in the response.

The requests (in python) and responses (from tcpdump) are as follows:

r = requests.post('http://freenasServer/api/v1.0/storage/volume/myvolume/datasets', auth=('user', 'password'), headers={'Content-Type': 'application/json', data={'name': 'apiTest'})

HTTP/1.1 301 MOVED PERMANENTLY
Server: nginx/1.6.2
Date: Fri, 07 Aug 2015 00:09:46 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Content-Language: en-us
Location: http://freenasServer/api/v1.0/storage/volume/myvolume/datasets/
Vary: Accept-Language

r = requests.post('http://freenasServer/api/v1.0/storage/volume/myvolume/datasets/', auth=('user', 'password'), headers={'Content-Type': 'application/json', data={'name': 'apiTest'})

HTTP/1.1 400 BAD REQUEST
Server: nginx/1.6.2
Date: Fri, 07 Aug 2015 00:56:43 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Connection: keep-alive
Content-Language: en
Vary: Accept-Language, Cookie
 

DrKK

FreeNAS Generalissimo
Joined
Oct 15, 2013
Messages
3,630
This user attempted to get help in the IRC, and no one in there really used the API. I'd appreciate it if someone with experience on the API can give this guy a hand...
 
D

dlavigne

Guest
I asked a dev and got this answer:

I think "data" needs to be a json string... json.dumps({ bla bla bla }. If that doesn't work and there is a real problem, /var/log/debug.log should show why.
 

SweetAndLow

Sweet'NASty
Joined
Nov 6, 2013
Messages
6,421
I would say make sure you use json.dumps and make sure you are using ssl or no ssl correctly depending on what the api needs. Again I haven't needed freenas api so I don't know if it requires ssl.

Json.dumps just makes the dic you are passing a string so you could quickly test it also.
 

DrKK

FreeNAS Generalissimo
Joined
Oct 15, 2013
Messages
3,630
Just for my own edification, I am curious about what the use-case is for the API? What kind of project are you doing where you wanted to use the API?
 

onlyanegg

Cadet
Joined
Aug 6, 2015
Messages
7
We're using FreeNAS as an NFS server and storing users' home directories on it. Each user get's his or her own dataset. Since we have other services to update in addition to FreeNAS, we want to script it all, hence the API.
 

DrKK

FreeNAS Generalissimo
Joined
Oct 15, 2013
Messages
3,630
That seems reasonable. Do you have it all working now?
 

onlyanegg

Cadet
Joined
Aug 6, 2015
Messages
7
Actually no. I'm having trouble with a different function now, so you may see another post from me soon! ;)
 
Status
Not open for further replies.
Top