CURL Command

136 views
Skip to first unread message

Soumen Khatua

unread,
Feb 4, 2020, 3:02:19 AM2/4/20
to django...@googlegroups.com
Hi Folks,

Actually now I'm usng POSTMAN for all operation like GET,POST,PUT,PATCH and DELETE. But I want to do the same thing by using Curl Command, Could anyone tell me what are the commands for CURL.I'm providing my end point below and most important I'm using Basic Authentication.

3) http://127.0.0.1:8000/job_post/ ---POST Operation 

Kasper Laudrup

unread,
Feb 4, 2020, 5:42:33 AM2/4/20
to django...@googlegroups.com
Hi Soumen,

On 04/02/2020 09.01, Soumen Khatua wrote:
> Hi Folks,
>
> Actually now I'm usng POSTMAN for all operation like GET,POST,PUT,PATCH
> and DELETE. But I want to do the same thing by using Curl Command, Could
> anyone tell me what are the commands for CURL.I'm providing my end point
> below and most important I'm using Basic Authentication.
>

The official CURL documentation seems like an obvious place to look:

https://curl.haxx.se/docs/

Kind regards,

Kasper Laudrup

Soumen Khatua

unread,
Feb 4, 2020, 6:15:47 AM2/4/20
to django...@googlegroups.com
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.

--
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/b84ba68c-993b-ea0b-7def-1bade72c8031%40stacktrace.dk.

Kasper Laudrup

unread,
Feb 4, 2020, 6:44:04 AM2/4/20
to django...@googlegroups.com
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

Soumen Khatua

unread,
Feb 4, 2020, 6:51:32 AM2/4/20
to django...@googlegroups.com
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

--
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.

Kasper Laudrup

unread,
Feb 4, 2020, 7:00:43 AM2/4/20
to django...@googlegroups.com
Hi Soumen,

Did you read the rest of my answer past the first two lines?

On 04/02/2020 12.50, Soumen Khatua wrote:
> 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)*
> *

Try comparing the output where you output the request data between what
is written when using POSTMAN and what is written when using CURL.

That should point you in the right direction.

Kind regards,

Kasper Laudrup

Soumen Khatua

unread,
Feb 4, 2020, 7:03:09 AM2/4/20
to django...@googlegroups.com
Yeah I did whatever you said but still I'm facing the same issue, But same code is working in Httpie and POSTMAN.That's why I'm surprised.


--
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.

Kasper Laudrup

unread,
Feb 4, 2020, 7:16:42 AM2/4/20
to django...@googlegroups.com
Hi Soumen,

On 04/02/2020 13.02, Soumen Khatua wrote:
> Yeah I did whatever you said but still I'm facing the same issue, But
> same code is working in Httpie and POSTMAN.That's why I'm surprised.
>

No you didn't.

Try to compare the POST data being sent using the different clients,
then it should become obvious at some point, but I cannot do that for
you. You have to figure that out yourself.

Kind regards,

Kasper Laudrup

Soumen Khatua

unread,
Feb 4, 2020, 7:26:42 AM2/4/20
to django...@googlegroups.com
Okay,sorry I thought Wireshark some code snippet. Now i got it.

Thank you

--
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.

Rak, Václav

unread,
Feb 4, 2020, 8:05:15 AM2/4/20
to django...@googlegroups.com
so check reuest header Content-Type: application/json 

this should be in request, check request headers in postman ;) 

Vena

Reply all
Reply to author
Forward
0 new messages