我想将json数据发布到api,但是api不能从应用程序接收数据,尽管我从php脚本测试了api并且它可以正常工作。有解决方案吗?
此api请求结构
--header 'Content-Type: application/json' \
--data-raw '{
"username": "some data",
"password": "some data",
"mobile": "some data",
"hash": "some data"
}'
这是抖动功能
static Future<String> createNewUser(String url,{String body}) async{
Map<String,String> headers = {
"Content-type" : "application/json",
};
return await http.post(url,body: body,headers: headers).then((http.Response response){
final int statusCode = response.statusCode;
print(response.body);
if( json == null){
throw new Exception("Error while create new account");
}
return response.body;
});
}
这是对象编码
CreateUser createUser = new CreateUser(
username: "somedata",
password:"somedata",
mobile: "somedata",
hash: Validation.generateHash("somedata"),
);
var body = jsonEncode(createUser.toMap());
CreateUser.createNewUser(Config.URL_CREATE_NEW_USER,body: body).then((res){
print(res);
});
这是php测试脚本
$data = array(
'username' => $username,
'password' => $password,
'mobile' => $mobile,
'hash' => $hash);
$json_data = json_encode($data);
$request_url = 'url here';
$ch = curl_init($request_url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($json_data))
);
$output = curl_exec($ch);
print_r($output);
这是我的后端php代码
谢谢。