【问题标题】:Posting JSON Array of Integers发布 JSON 整数数组
【发布时间】:2018-03-26 15:36:17
【问题描述】:

我正在尝试将一些 JSON 数据发布到 API 以添加帐户。 说明指定 ids 参数可以是:字符串(逗号分隔),或整数数组

我意识到我可以将逗号分隔的 id 放入查询字符串中,但是我想将此数据作为 JSON 发布,因为我可能有大量这样的数据。

这是我尝试过的:

public static HttpClient GetHttpClient()
{
    var property = Properties.Settings.Default;

    HttpClient client = new HttpClient();
    client.BaseAddress = new Uri(property.apiUrl);
    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    client.DefaultRequestHeaders.Add("X-OrgSync-API-Key", property.apiKey);
    return client;
}

HttpClient client = Api.GetHttpClient();
string json = "{\"ids\":[10545801,10731939]}";
var httpContent = new StringContent(json, Encoding.UTF8, "application/json");
var response = await client.PostAsync($"{client.BaseAddress}/classifications/{classification.id}/accounts/add", httpContent);

它“成功”运行,但实际上在 API 服务器端没有设置任何内容。

关于我在这里可能做错了什么有什么想法吗? 此外,任何类型的工具/技术等,尤其是在 Visual Studio 中,可以让我更好地了解请求/响应流量?

我知道这是可能的,因为当我使用 Postman 之类的工具时,它会正确添加帐户 ID:

【问题讨论】:

  • 那么 API 的动作签名是什么?
  • 您是否也提供了其他必需的参数?
  • 我在设置 HttpClient 时。我将此代码添加到我原来的问题中。
  • 我发现如果我在设置 StringContent 时将第二个参数更改为 null 或 Encoding.Default 而不是 Encoding.UTF8 ,它可以正常工作。我不知道为什么。

标签: c# json http-post httpresponse dotnet-httpclient


【解决方案1】:

关于工具/技术,您可以使用Fiddler 即时捕获请求和响应,以检查原始请求是否正确。

如果您以前没有使用过它,请查看 here 以获取有关如何捕获请求和响应的说明。

【讨论】:

  • 谢谢,我一定会调查的。
【解决方案2】:

我能够通过将 StringContent 编码类型从 Encoding.UTF8 更改为 null 或 Encoding.Default 来使 json 字符串方法工作。

string json = "{\"ids\":[10545801,10731939]}";
var httpContent = new StringContent(json, Encoding.Default, "application/json");
var response = await client.PostAsync($"{client.BaseAddress}/classifications/{classification.id}/accounts/add", httpContent);

我还想出了一种方法,可以使用包含带有编码.UTF8 的 int 数组的对象;

HttpClient client = Api.GetHttpClient();
var postData = new PostData {ids = new[] {10545801,10731939}};
var json = JsonConvert.SerializeObject(postData);
var httpContent = new StringContent(json, Encoding.UTF8, "application/json");
var response = await client.PostAsync($"{client.BaseAddress}/classifications/{classification.id}/accounts/add", httpContent);

如果您不想麻烦地创建一个类来存储帖子数据,您可以使用匿名类型:

var postData = new { ids = new[] {10545801,10731939}};
var json = JsonConvert.SerializeObject(postData);
var httpContent = new StringContent(json, Encoding.UTF8, "application/json");
var response = await client.PostAsync($"{client.BaseAddress}/classifications/{classification.id}/accounts/add", httpContent);

【讨论】:

    【解决方案3】:

    试试下面的代码就行了

    using (var client= new HttpClient()) {
    string json = "{\"ids\":[10545801,10731939]}";
    var httpContent = new StringContent(json, Encoding.UTF8, "application/json");
    var response = await client.PostAsync($"{client.BaseAddress}/classifications/{classification.id}/accounts/add", httpContent);
    
     // If the response contains content we want to read it!
        if (response .Content != null) {
            var responseContent = await response.Content.ReadAsStringAsync();
    //you will get your response in responseContent 
    }
    

    【讨论】:

      猜你喜欢
      • 2017-08-26
      • 1970-01-01
      • 2011-12-15
      • 2018-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多