calling CURL with POST data and headers on command line

16,057

How can i call same URL using command line only?

I didn't write out all the data pairs, but the following should get you started. I suggest reading more on curl

curl -H 'Content-Type: application/json' -X POST -d '{"username": "dtthtdas45", "password": "123456"}' http://localhost:8000/api/ecp/user/?format=json

Note: Assuming you are doing this for more endpoints, you might want to check out a tool like resty.

Share:
16,057

Related videos on Youtube

hangman
Author by

hangman

Updated on June 04, 2022

Comments

  • hangman
    hangman almost 2 years

    I am using RESTAPI to communicate php client with django server. I have posted json data. The php code is

    $arr=array("username"=>"dtthtdas45", 
              "password"=>"123456", 
              "email"=>"[email protected]", 
              "is_active"=>"1", 
              "is_staff"=>"1", 
              "is_superuser"=>"1",
              "promo_code"=>"1212121",
              "gender"=>"m",
              "birth_year"=>"1991",
              "zip"=>"77707",
              "first_name"=>"john",
              "last_name"=>"doe",
              "current_state"=>"1"
              );
    echo $data_string= json_encode($arr);
    $ch = curl_init('http://localhost:8000/api/ecp/user/?format=json');                                                                      
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
        'Content-Type: application/json',                                                                                
        'Content-Length: ' . strlen($data_string))                                                                       
    );                                                                                                                   
     $result = curl_exec($ch);
    

    How can i call same URL using command line only?

    I tried the folowing

    curl -H 'Content-Type: application/json' -X POST -d '{"username": "dtthtdas45", "password": "123456","email":"[email protected]","is_active":"1","is_staff":"1","is_superuser",promo_code":"1212121","gender":"m","birth_year":"1991","zip":"77707","first_name":"john","last_name":"doe","current_state":"1"}' http://localhost:8000/api/ecp/user/?format=json
    

    but no luck , it shows the folowing error

    curl: (6) Couldn't resolve host 'application'
    curl: (6) Couldn't resolve host 'dtthtdas45,'
    curl: (6) Couldn't resolve host 'password:'
    
  • hangman
    hangman over 11 years
    i tried same, but the postdata should be in json format! , any clue?