0

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": "ceciestuntest@datasystems.fr",
}

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

print (response.text)
Essex
  • 6,042
  • 11
  • 67
  • 139
  • No server code? No error message? No traceback? – Klaus D. Feb 20 '18 at 08:15
  • No, because I'm executing my script outside of Django perimeter. I created a file `API_create.py` with the content above, and I make : `python API_create.py`. It works with other files : API_list, API_details, etc ... But I don't overcome to create an object thanks to this method. – Essex Feb 20 '18 at 08:18
  • post the create code so that its easier to understand, and also any tracebacck – Exprator Feb 20 '18 at 08:19
  • @Exprator I can post the create code, but I don't have Traceback because I'm executing my python file with my Terminal ^^ – Essex Feb 20 '18 at 08:21
  • You probably have to use `post` to create a new object, so your code looks good. I think you should `print(response)` to get the status code. There is probably something wrong in the posted data that explains the problem. – luc Feb 20 '18 at 08:24
  • Are you sure that your your url is correct? – luc Feb 20 '18 at 08:25
  • @luc I'm getting `` so I have to see what is this error. But you're right, the url had a little mistake. – Essex Feb 20 '18 at 08:28

2 Answers2

1

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/

luc
  • 41,928
  • 25
  • 127
  • 172
  • I'm getting this error with `print(response.text)` : `{"Image":["Data is not a file Check the formulary encoding type."]}`. It's pretty weird because the file is good and the path too : `"Image": "/Users/valentin/Desktop/Django/DatasystemsCORE/Media/pictures/photo.jpg",` – Essex Feb 20 '18 at 08:39
  • @Deadpool The server expects a file and you give it a link to a file. May be this will help : https://stackoverflow.com/a/22567429/117092 – luc Feb 20 '18 at 08:41
0

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)
Essex
  • 6,042
  • 11
  • 67
  • 139