HttpClient PutAsync doesn't send a parameter to api

前端 未结 2 636
感情败类
感情败类 2021-01-04 13:08

On the controller Put is as following:

[HttpPut]
[ActionName(\"putname\")]
public JsonResult putname(string name)
{
    var response = ...
    return Json(re         


        
2条回答
  •  悲哀的现实
    2021-01-04 13:36

    For me it worked correctly:

                string requestUrl = endpointUri + "/Files/";
                var jsonString = JsonConvert.SerializeObject(new { name = "newFile.txt", type = "File" }); 
    
                HttpContent httpContent = new StringContent(jsonString);
                httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue ("application/json");          
    
                HttpClient hc = new HttpClient();
    
                //add the header with the access token
                hc.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", accessToken);
    
                //make the put request
                HttpResponseMessage hrm = (await hc.PostAsync(requestUrl, httpContent));
    
                if (hrm.IsSuccessStatusCode)
                {
                   //stuff
                }
    

提交回复
热议问题