http post request 415: unsupportesd media

300 views
Skip to first unread message

Harsh Tibrewal

unread,
Mar 16, 2020, 6:57:06 AM3/16/20
to Django users
I am trying to send a post request in django rest API with mongoengine, while sending a POST request with the following data an 415 status code is returned

{
  orderId
: sample2,
  restaurantId
: cartssj,
  dateTime
: fdsfd,
  status
: pending,
  cartItems
: [
   
{
     
"itemId": "1001",
     
"itemName": "Onion",
     
"requestedQty": "1",
     
"fulfilledQty": "1",
     
"pricePerUnit": "78"
   
},
   
{
     
"itemId": "1002",
     
"itemName": "Potato",
     
"requestedQty": "1",
     
"fulfilledQty": "1",
     
"pricePerUnit": "35"
   
},
   
{
     
"itemId": "1003",
     
"itemName": "Carrot",
     
"requestedQty": "1",
     
"fulfilledQty": "1",
     
"pricePerUnit": "78"
   
},
   
{
     
"itemId": "1004",
     
"itemName": "Toamto",
     
"requestedQty": "1",
     
"fulfilledQty": "1",
     
"pricePerUnit": "90"
   
}
 
],
  total
: 281.0
}

the models have been defined as follows ini django:


from django.db import models
from mongoengine import Document,fields,EmbeddedDocument,EmbeddedDocumentListField


class CartItem(EmbeddedDocument):
    itemId
= fields.StringField(max_length=254,uniique=True)
    itemName
= fields.StringField(max_length=254)
    requestedQty
= fields.StringField(max_length=100)
    fulfilledQty
= fields.StringField(max_length=100)
    pricePerUnit
= fields.StringField(max_length=100)






class Order(Document):
    orderId
= fields.StringField(max_length=254)
    restaurantId
= fields.StringField(max_length=100)
    dateTime
= fields.StringField(max_length=254)
    status
= fields.StringField(max_length=254)
    cartItems
= fields.EmbeddedDocumentListField(CartItem)
    total
= fields.StringField(max_length=254)
    meta
= {'collection': 'orders'}


PLease help me solve this error

Onyore Daniel

unread,
Mar 16, 2020, 12:30:44 PM3/16/20
to Django users
working on it

Harsh Tibrewal

unread,
Mar 16, 2020, 2:27:23 PM3/16/20
to django...@googlegroups.com
Hi, iwas able to get the right format of json to be passed finally but still django returns an  415 Unsupported Media Type Error
the following data is passed, it is the right format and is getting added when using the restframework raw data form but not getting added from post request

{
  "orderId": "sample2",
  "restaurantId": "cartssj",
  "dateTime": "fdsfd",
  "status": "pending",

  "cartItems": [
    {
      "itemId": "1001",
      "itemName": "Onion",
      "requestedQty": "1",
      "fulfilledQty": "1",
      "pricePerUnit": "78"
    },
    {
      "itemId": "1002",
      "itemName": "Potato",
      "requestedQty": "1",
      "fulfilledQty": "1",
      "pricePerUnit": "35"
    }
  ],
  "total": "113.0"
}

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a6529746-9471-4f98-9d4c-bf6b508bb0b3%40googlegroups.com.

Harsh Tibrewal

unread,
Mar 16, 2020, 3:27:17 PM3/16/20
to Django users
leave it found the error and fixed actually i had to pass a header in it and format the data properly
Reply all
Reply to author
Forward
0 new messages