Reputation:
I want to post data via an api onto my django app. This is how far I got:
import pandas
import requests
excel_data_df = pandas.read_excel('workorders.xlsx')
json_str = excel_data_df.to_json(orient='records', date_format='iso')
API_ENDPOINT = "http://127.0.0.1:8000/api/create/"
API_KEY = "dF8NbXRA.94Mj2xeXT3NZOtx1b575CvNvbs8JWo0D"
data = {'api_dev_key':API_KEY,
'api_option':'paste',
'api_paste_code':json_str ,
'api_paste_format':'csv'}
r = requests.post(url = API_ENDPOINT, data = data)
Django
views.py
class PostDataView(CreateAPIView):
queryset = Workorder.objects.all()
serializer_class = WorkorderSerializer
serializers.py
class WorkorderSerializer(serializers.ModelSerializer):
class Meta:
model = Workorder
exclude = ['id']
urls.py
from django.conf.urls import url, include
from .views import *
from rest_framework.urlpatterns import format_suffix_patterns
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
url(r'^api/chart/data/$', ChartData.as_view(),name="api-data"),
url(r'^api/create/$', PostDataView.as_view(), name='create'),
url(r'^(?P<pk>\d+)/api/delete/$', DeleteDataView.as_view(), name='delete'),
url(r'^(?P<pk>\d+)/api/update/$', UpdateDataView.as_view(), name='update'),
url(r'^$', display_mobiles, name="display_mobiles"),
url(r'^(?P<pk>\d+)$', edit_mobile, name="edit_mobile"),
url(r'^delete/(?P<pk>\d+)$', delete_mobile, name="delete_mobile"),
url(r'^home/$', index, name='index'),
url(r'^sitemap/$', sitemap, name='sitemap'),
url(r'^upload/$', upload, name='upload'),
url(r'^test/$', testview, name='test')
]
if settings.DEBUG:
urlpatterns = urlpatterns + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns = urlpatterns + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns = format_suffix_patterns(urlpatterns)
models.py
class Workorder(models.Model):
label = models.CharField(max_length=200, blank=False)
start = models.DateField()
end = models.DateField()
duration = models.IntegerField()
ctype = models.CharField(max_length=20, default='bar')
werk = models.CharField(max_length=50, default='plant')
product = models.CharField(max_length=50)
train_number = models.IntegerField()
latest_start_date = models.DateField()
latest_start_timebucket = models.IntegerField()
is_start_date_fixed = models.BooleanField()
assigned_start_timebucket = models.IntegerField()
assigned_start_date = models.DateField()
costs_early_start = models.IntegerField()
costs_late_start = models.IntegerField()
resource_overall_demands = models.IntegerField()
resource_timeslots_demands = models.IntegerField()
I can enter the data manually using the post forms @ api/create but when I try to post it via the api, I get this error:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>Error response</title>
</head>
<body>
<h1>Error response</h1>
<p>Error code: 400</p>
<p>Message: Bad Request.</p>
<p>Error code explanation: 400 - Bad request syntax or unsupported method.</p>
</body>
</html>
This is the data I tried to post:
1)
[
{"id":null,"label":"Workorder 1","start":"2019-01-01T00:00:00.000Z","end":"2019-01-10T00:00:00.000Z","duration":9,"ctype":"bar","werk":"a","product":"a","train_number":535435.0,"latest_start_date":"2019-01-10T00:00:00.000Z","latest_start_timebucket":9,"is_start_date_fixed":false,"assigned_start_timebucket":535435.0,"assigned_start_date":"2019-01-10T00:00:00.000Z","costs_early_start":334,"costs_late_start":334,"resource_overall_demands":334,"resource_timeslots_demands":334},
{"id":null,"label":"Workorder 1","start":"2019-01-01T00:00:00.000Z","end":"2019-01-10T00:00:00.000Z","duration":9,"ctype":"bar","werk":"a","product":"a","train_number":535435.0,"latest_start_date":"2019-01-10T00:00:00.000Z","latest_start_timebucket":9,"is_start_date_fixed":false,"assigned_start_timebucket":535435.0,"assigned_start_date":"2019-01-10T00:00:00.000Z","costs_early_start":334,"costs_late_start":334,"resource_overall_demands":334,"resource_timeslots_demands":334}
]
2)
[
{"id":null,"label":"Workorder 1","start":"2019-01-01T00:00:00.000Z","end":"2019-01-10T00:00:00.000Z","duration":9,"ctype":"bar","werk":"a","product":"a","train_number":535435,"latest_start_date":"2019-01-10T00:00:00.000Z","latest_start_timebucket":9,"is_start_date_fixed":false,"assigned_start_timebucket":535435,"assigned_start_date":"2019-01-10T00:00:00.000Z","costs_early_start":334,"costs_late_start":334,"resource_overall_demands":334,"resource_timeslots_demands":334}
]
3)
{"id":null,"label":"Workorder 1","start":"2019-01-01T00:00:00.000Z","end":"2019-01-10T00:00:00.000Z","duration":9,"ctype":"bar","werk":"a","product":"a","train_number":535435,"latest_start_date":"2019-01-10T00:00:00.000Z","latest_start_timebucket":9,"is_start_date_fixed":false,"assigned_start_timebucket":535435,"assigned_start_date":"2019-01-10T00:00:00.000Z","costs_early_start":334,"costs_late_start":334,"resource_overall_demands":334,"resource_timeslots_demands":334}
4)
{"id":1}
Thank you for any help
Upvotes: 1
Views: 1750
Reputation: 2852
I don't understand what the api_...
are referring to.
But given the error message, it seems that your client is not sending a proper request.
One usefull way of debugging this is to try to make it work first with generated code from the devtools:
Network
, select All
copy as fetch
or copy as curl
Upvotes: 0
Reputation: 3981
When you do requests.post(url = API_ENDPOINT, data = data)
it sends yoru data as form data.
It looks like you are trying to send data in a format which your API doesn't accept.
I think your API is configured to accept only JSON but you are trying to send it as form data.
Check settings from the docs at https://www.django-rest-framework.org/api-guide/parsers/#setting-the-parsers
Also you can try to post your data as JSON strings using requests.post(..., json=payload)
. Reference https://requests.readthedocs.io/en/master/user/quickstart/#more-complicated-post-requests
Upvotes: 1