"dtthtdas45", -6ren">
gpt4 book ai didi

php - 在命令行上使用 POST 数据和 header 调用 CURL

转载 作者:IT王子 更新时间:2023-10-29 00:36:16 27 4
gpt4 key购买 nike

我正在使用 RESTAPI 将 php 客户端与 django 服务器通信。我已经发布了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:'

最佳答案

How can i call same URL using command line only?

我没有写出所有数据对,但下面的内容应该可以帮助您入门。我建议阅读更多关于 curl 的内容

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

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

关于php - 在命令行上使用 POST 数据和 header 调用 CURL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12822879/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com