与命令行POST数据和标题调用CURL(calling CURL with POST data an

2019-08-01 07:33发布

我使用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:'

Answer 1:

我怎么能只使用命令行调用同一个URL?

我没有写出来的所有数据对,但下面应该让你开始。 我建议您阅读更多的curl

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

注意:假设你正在做这个更多的端点,你可能想看看像工具resty 。



文章来源: calling CURL with POST data and headers on command line