Essex
Essex

Reputation: 6138

Django REST API : Create object from python file

I'm looking for developping Django REST API from my web application and I would like to try creating object through the API. Mainly if my process is well-implemented.

I'm using a python file in order to make that.

This is my file :

import requests

url = 'http://localhost:8000/Api/identification/create/'

data = {
    "Etat": "Vivant",
    "Civilite": "Monsieur",
    "Nom": "creation",
    "Prenom": "via-api",
    "Sexe": "Masculin",
    "Statut": "Célibataire",
    "DateNaissance": "1991-11-23",
    "VilleNaissance": "STRASBOURG",
    "PaysNaissance": "FR",
    "Nationalite1": "FRANCAISE",
    "Nationalite2": "",
    "Profession": "Journaliste",
    "Adresse": "12, rue des fleurs",
    "Ville": "STRASBOURG",
    "Zip": 67000,
    "Pays": "FR",
    "Mail": "",
    "Telephone": "",
    "Image": "http://localhost:8000/media/pictures/HH_Hh_19212-00001-979812-2_ShUDIk8.jpg",
    "CarteIdentite": "http://localhost:8000/media/Carte_Identite/carte_ID_lzpOI41_WOnC9WH.gif"
    }

response = requests.post(url, data=data)

print (response.text)

I'm confusing if I have to use post or put, but anyway, I don't overcome to create my object. If I make the process directly through the API, it works, but not with a pythonic file.

Any idea ?

EDIT :

For example, it works with these files :

#API_list.py
import requests

url = 'http://localhost:8000/Api/Identification/'

response = requests.get(url)

print (response.text)

and

#API_details.py
import requests

url = 'http://localhost:8000/Api/Identification/26/'

response = requests.get(url)

print (response.text)

and

#API_edit.py
import requests

url = 'http://localhost:8000/api/Identity/26/edit/'

data = {
    "Ville": "STRASBOURG",
    "Zip": 67000,
    "Pays": "FR",
    "Adresse": "12, rue de la mésange",
    "Telephone": "0388603938",
    "Mail": "[email protected]",
}

response = requests.put(url, data=data)

print (response.text)

Upvotes: 0

Views: 422

Answers (2)

Essex
Essex

Reputation: 6138

I found the solution thanks to @luc answer :

import requests

url = 'http://localhost:8000/Api/Identification/create/'

filename1 = '/Users/valentin/Desktop/Django/DatasystemsCORE/Media/pictures/photo.jpg'
filename2 = '/Users/valentin/Desktop/Django/DatasystemsCORE/Media/Carte_Identite/carte_ID.gif'
files = {'Image' : open(filename1,'rb'), 'CarteIdentite': open(filename2,'rb')}

data = {
    "Etat": "Vivant",
    "Civilite": "Monsieur",
    "Nom": "creation",
    "Prenom": "via-api",
    "Sexe": "Masculin",
    "Statut": "Célibataire",
    "DateNaissance": "1991-11-23",
    "VilleNaissance": "STRASBOURG",
    "PaysNaissance": "FR",
    "Nationalite1": "FRANCAISE",
    "Nationalite2": "",
    "Profession": "JJJ",
    "Adresse": "12, rue des fleurs",
    "Ville": "STRASBOURG",
    "Zip": 67000,
    "Pays": "FR",
    "Mail": "",
    "Telephone": ""
    }

response = requests.post(url, files=files, data=data)

print(response.text)

Upvotes: 0

luc
luc

Reputation: 43116

You probably have to use post to create a new object, so your code looks good.

There is probably something wrong in the posted data that explains the problem.

I think you should print(response) to get the status code. That will help to understand. Look also at your server logs, that may tell you what is wrong.

Maybe there is also something wrong in your url. If you are using a 'Django-rest-framework' viewset, the url for a create is probably something like http://localhost:8000/Api/identification/

Upvotes: 1

Related Questions