我想将json数据发布到api,但是api不能从应用程序接收数据,尽管我从php脚本测试了api并且它可以正常工作。 “我认为Content-Type中的问题:application / json”,但我在代码中进行了设置。有解决方案吗?
--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;
});
}
编码的json
CreateUser createUser = new CreateUser(
username: "someData",
password:"someData",
mobile: "someData",
hash: Validation.generateHash("someData"),
);
var body = json.encode(createUser.toMap());
CreateUser.createNewUser(Config.URL_CREATE_NEW_USER,body: body).then((res){
print(res);
答案 0 :(得分:0)
尝试不要在一个await
值上同时使用then
和Future
。
尝试以下操作:
static Future<String> createNewUser(String url,{String body}) async{
Map<String,String> headers = {
"Content-type" : "application/json",
};
http.Response response = await http.post(url,body: body,headers: headers)
final int statusCode = response.statusCode;
print(response.body);
if( json == null){
throw new Exception("Error while create new account");
}
return response.body;
}