Tuesday, February 4, 2020

Re: CURL Command

My views.py file

    def post(self, request,*args, **kwargs):
        """
        Create a Job record
        :param format: Format of the Job records to return to
        :param request: Request object for creating Job records
        :return: Returns a Job record
        """
        print("request.data:",request.data)
        serializer = JobSerializer(data = request.data)

        if serializer.is_valid(raise_exception = ValueError):
            serializer.save(recruiter = self.request.user)
            return Response(serializer.data, status = status.HTTP_201_CREATED)
        return Response(serializer.error_messages,status = status.HTTP_400_BAD_REQUEST)



serializers.py file


class JobSerializer(serializers.ModelSerializer):
    seekers_name = RegisterSerializer(required = False)
    class Meta:
        model = Job
        fields = ('id','seekers_name','job_description','is_walk_in','post_date')

    def create(self,validated_data):
        """
        Overriding the default create method of the Model serializer.
        :param validated_data: data containing all the details of Job
        :return: returns a successfully created job record
        """
        seekers_data = validated_data.pop('seekers_name',None)
        return Job.objects.create(**validated_data)
        return validated_data


But it's working in POSTMAN Tool

Thank you for your response

On Tue, Feb 4, 2020 at 5:13 PM Kasper Laudrup <laudrup@stacktrace.dk> wrote:
Hi Soumen,

On 04/02/2020 12.14, Soumen Khatua wrote:
> curl -i -X POST -H "Authorization: Basic Nzc5NzcyNzU0MDpzb3VtZW4xMjM="
> http://127.0.0.1:8000/job_post/ --data '{"job_description":"Python
> Developers"}'
>
> Here I'm passing the data of job_description but still I'm getting an
> error like:
> *
> *
> *HTTP/1.1 400 Bad Request
> Date: Tue, 04 Feb 2020 11:12:05 GMT
> Server: WSGIServer/0.2 CPython/3.8.1
> Content-Type: application/json
> Vary: Accept
> Allow: GET, POST, PUT, DELETE, HEAD, OPTIONS
> X-Frame-Options: SAMEORIGIN
> Content-Length: 47
>
> {"job_description":["This field is required."]}*
>
>
> Could you tell me why I'm getting this error?
> but in POSTMAN it is woking properly.
>

That's impossible to answer without seeing your actual code, but a few
hints:

You are not setting the content type when posting. Try setting that to
JSON. It could be the error message that is just confusing and it's not
trying to parse the input as JSON, but you should not that better if you
wrote the code.

The payload you are posting definitely looks like JSON, but I have no
idea which structure your code is expecting.

It can often be useful to sniff the traffic to see what is actually
being sending when debugging stuff like this. A tool like wireshark is
extremely useful for that:

https://www.wireshark.org/

Happy debugging,

Kasper Laudrup

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/71ae3f30-cba1-a275-1cd0-7155063be0ab%40stacktrace.dk.

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAPUw6WZgZzSXQxxTxXuHTLN9rOhCGK3p_FZOTYx%3Dp%2BiP_C566A%40mail.gmail.com.

No comments:

Post a Comment