I am trying to create a POST api using tastypie. It is working fine when I am running from postman. But when I created django tests for the same, the data is not coming in request.POST
instead it is coming in request.body
. Is there a way to test it similar to postman using django tests? I know it is coming in request.body
but why is it working from postman? I followed this(https://django-tastypie.readthedocs.org/en/latest/testing.html) also but same issue.
api.py
class CustResource(Resource):
class Meta:
resource_name = 'customer'
authentication = ApiKeyAuthentication()
def prepend_urls(self):
return [
url(r"^(?P<resource_name>%s)/register%s$" %
(self._meta.resource_name, trailing_slash()),
self.wrap_view('register_me'), name="api_register_me")
]
def register_me(self, request, **kwargs):
self.method_check(request, allowed=['post'])
self.is_authenticated(request)
response = {"status": 'error', 'data':{'error_code': '9999'},
'message':'some issue'}
return self.create_response(request, response)
tests.py
class RestApiTests(TestCase):
def setUp(self):
"""
create
"""
self.user = User.objects.create_user(username="anuj",email="anuj@gmail.com",password="testing")
self.apikey = ApiKey.objects.get(user=self.user)
def test1(self):
"""
"""
client = Client()
json_str = json.dumps({'format':'json','username':'anuj','api_key':'fc32bd921bb6386c945229e675704818b29dadf0'})
response = client.post('/api/v1/customer/register/',data=json_str, content_type='application/json')
print response.status_code
self.assertEqual(response.status_code, 200, msg="Wrong status code")