简体   繁体   English

如何使用HttpClient将JSON数据发布到Web API

[英]How to Post JSON data to a Web API using HttpClient

I have the following code, basically it takes in a dynamic object (in this case of type file) and using the HTTPClient class tries to a POST to a WebAPI controller , the issue I am having is that the controller is always getting NULL for the values on my [FromBody] parameter. 我有以下代码,基本上它接受一个动态对象(在这种情况下是类型文件)并使用HTTPClient类尝试POST到WebAPI controller ,我遇到的问题是控制器始终为NULL获取我的[FromBody]参数的值。

Code

var obj = new
        {
            f = new File
            {
                Description = description,
                File64 = Convert.ToBase64String(fileContent),
                FileName = fileName,
                VersionName = versionName,
                MimeType = mimeType
            },
        }

var client = new HttpClient(signingHandler)
{
   BaseAddress = new Uri(baseURL + path) //In this case v1/document/checkin/12345
};

client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));                        

HttpResponseMessage response;
action = Uri.EscapeUriString(action);

//Obj is passed into this, currently it is of type File 
var content = new StringContent(JsonConvert.SerializeObject(obj).ToString(),
            Encoding.UTF8, "application/json");

response = client.PostAsync(action, content)).Result;
if (response.IsSuccessStatusCode)
{     
    var responseContent = response.Content;                
    string responseString = responseContent.ReadAsStringAsync().Result;
    return JsonConvert.DeserializeObject<T>(responseString);
}

Controller 调节器

[HttpPost]
[Route("v1/document/checkin/{id:int}")]
public void Checkin_V1(int id, [FromBody] File f)
{
        //DO STUFF - f has null on all of its properties
}

Model 模型

public class File
{
    public string FileName { get; set; }
    public string VersionName { get; set; }
    public string Description { get; set; }
    public string MimeType { get; set; }
    public byte[] Bytes { get; set;}
    public string File64 { get; set; }
}

The model is shared on both the WebAPI and the client app. 该模型在WebAPI和客户端应用程序上共享。

Any help on why this is failing would be much appreciated, been going around in circles for a while now. 任何关于为什么失败的帮助都会非常感激,现在已经在圈子里走了一段时间。

Your obj right at the start isn't needed. 不需要你在开始时的obj。 That is nesting f inside another object. 那就是将f嵌套在另一个对象中。

var obj = new
    {
        f = new File
        {
            Description = description,
            File64 = Convert.ToBase64String(fileContent),
            FileName = fileName,
            VersionName = versionName,
            MimeType = mimeType
        },
    }

Change to 改成

var f = new File
{
    Description = description,
    File64 = Convert.ToBase64String(fileContent),
    FileName = fileName,
    VersionName = versionName,
    MimeType = mimeType
};

Then just serialize f. 然后只序列化f。

I think there is a problem on this part of your code 我认为这部分代码存在问题

    var obj = new
    {
        f = new File
        {
            Description = description,
            File64 = Convert.ToBase64String(fileContent),
            FileName = fileName,
            VersionName = versionName,
            MimeType = mimeType
        },
    }

As this will be serialized differently from what you really needed. 因为这将与您真正需要的不同而序列化。 Try this instead 试试这个

   var obj =  new File
        {
            Description = description,
            File64 = Convert.ToBase64String(fileContent),
            FileName = fileName,
            VersionName = versionName,
            MimeType = mimeType
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM