2016-10-24 70 views
0

我正在構建一個asp.net核心應用程序與debfault aspnetidentitycore plugedin,唯一的變化是我通過修改regiester方法到api添加了一個動作,這意味着可以被另一個應用程序調用。aspnet核心應用程序奇怪的請求類型

[HttpPost] 
    [AllowAnonymous] 
    public async Task<EngineResult<object>> RegisterByApi([FromBody]RegisterViewModel model) 
    { 
     if (ModelState.IsValid) 
     { 
      var user = new IdentityUser 
      { 
       DisplayName = model.Name, 
       UserName = model.Email, 
       Email = model.Email, 
       PhoneNumber = model.PhoneNumber, 
      }; 

      var result = await _userManager.CreateAsync(user, model.Password); 
      if (result.Succeeded) 
      { 
       await _signInManager.SignInAsync(user, isPersistent: false); 
       _logger.LogInformation(3, "User created a new account with password."); 
       return new EngineResult<object>(true) { Entity = new { sub = user.Id, name = user.DisplayName } }; 
      } 
      return new EngineResult<object>(false) { Entity = string.Join(";", result.Errors?.Select(e => e.Description)) }; 
     } 

     return new EngineResult<object>(false) { Entity = string.Join(";", ModelState.Values.SelectMany(m => m.Errors).Select(e => e.ErrorMessage)) }; 
    } 

但是當PHP調用此API,我得到一個奇怪的內容類型,這是 應用/ JSON的;邊界= ------------------------ e3f0ef0cc3e74f25

這裏是PHP代碼

<?php 
$data = [ 
    "Name"=>"testname", 
    "Email"=>"[email protected]", 
    "Password"=>"123qwe!#QWE", 
    "ConfirmPassword"=>"[email protected]#QWE", 
    "PhoneNumber"=>"12312321321", 
]; 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, "http://xxxx/Account/RegisterByApi"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_POST, 1); 
header_remove(); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json' 
    ) 
); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
$output = curl_exec($ch); 
curl_close($ch); 
echo $output; 

人知道這裏發生了什麼?

回答

0

終於,這是我的php代碼問題,即不使用json數據,用轉換爲josn替換後數據將解決此問題: $ payload = json_encode($ data);