Problem accessing the API

Status
Not open for further replies.

Thibaut

Dabbler
Joined
Jun 21, 2014
Messages
33
Hello,

I'm new to FreeNAS API usage and just starting to play around with it.

I'm willing to set up a web interface that would mainly let registered users manage iSCSI targets, extents and target to extents. I've successfully managed to have the volumes, targets, extents... listed using FreeNAS API using the following python code in my views.py file:

Code:
from django.shortcuts import render
 
def index(request):
    # FreeNAS API query
    import requests
    import json
    from django.http import HttpResponse
 
    api_url = 'http://123.123.123.123/api/v1.0/'
    login = ('root', '123456')
 
    vols = requests.get(
        api_url + 'storage/volume/',
        auth=login,
        params={'offset': 0, 'limit': 10000}
    )
 
    targs = requests.get(
        api_url + 'services/iscsi/target/',
        auth=login,
        params={'offset': 0, 'limit': 10000}
    )
 
    exts = requests.get(
        api_url + 'services/iscsi/extent/',
        auth=login,
        params={'offset': 0, 'limit': 10000}
    )
   
    luns = requests.get(
        api_url + 'services/iscsi/targettoextent/',
        auth=login,
        params={'offset': 0, 'limit': 10000}
    )
 
    vols_json = vols.json()
    targs_json = targs.json()
    exts_json = exts.json()
    luns_json = luns.json()
 
    ...


What I'm willing to do now is use the POST method to create new entries, but this doesn't seem to work. Here's what I get from the python shell:

Code:
>>> import requests
>>> import json
>>> api_url = 'http://123.123.123.123/api/v1.0/'
>>> login = ('root', '123456')
>>> r = requests.post(api_url + 'services/iscsi/targettoextent/', auth=login, params={"iscsi_target": 2, "iscsi_extent": 2})
>>> r.text
u'{"error_message": "Sorry, this request could not be processed. Please try again later."}'


Same goes for PUT requests...

What am I missing ?
Any help would be appreciated.

Thank you.
 

William Grzybowski

Wizard
iXsystems
Joined
May 27, 2011
Messages
1,754
You're providing the data for the request as simple HTTP url parameters, which is wrong, "params" doesn't do what you think it does.
You need to provide it as part of the payload.


Code:
r = requests.post(
    url,
    auth=('root', 'freenas'),
    headers={'Content-Type': "application/json"},
 
    data=json.dumps({
        "iscsi_target": 2, "iscsi_extent": 2
    }),
 
)
 

Thibaut

Dabbler
Joined
Jun 21, 2014
Messages
33
Thanks a lot William!

Now I get it:

Code:
>>> r = requests.post( api_url + 'services/iscsi/targettoextent/', auth=login, headers={'Content-Type': "application/json"}, data=json.dumps({"iscsi_target": 2, "iscsi_extent": 2}),)
>>> r.json()
{u'iscsi_target': 2, u'iscsi_extent': 2, u'iscsi_lunid': None, u'id': 2}


I should probably have read further in python's requests module documentation, sorry :-/
 
Status
Not open for further replies.
Top