在命令行上使用POST数据和标头调用CURL

时间:2012-10-10 15:21:50

标签: php linux shell curl

我正在使用RESTAPI与django服务器通信php客户端。我发布了json数据。 php代码是

$arr=array("username"=>"dtthtdas45", 
          "password"=>"123456", 
          "email"=>"ramg@ram.com", 
          "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);

如何仅使用命令行调用相同的URL?

我尝试了下面的

curl -H 'Content-Type: application/json' -X POST -d '{"username": "dtthtdas45", "password": "123456","email":"email@email.com","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

但没有运气,它显示了以下错误

curl: (6) Couldn't resolve host 'application'
curl: (6) Couldn't resolve host 'dtthtdas45,'
curl: (6) Couldn't resolve host 'password:'

1 个答案:

答案 0 :(得分:3)

  

如何仅使用命令行调用相同的URL?

我没有写出所有数据对,但以下内容应该让你开始。我建议您在curl

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

注意:假设您为更多端点执行此操作,您可能需要查看resty之类的工具。