这个请求有什么问题(Azure management.azure.com API)

时间:2017-10-31 19:34:41

标签: c# azure http httpclient azure-api-management

我正在努力让这个天蓝色的API工作:

https://docs.microsoft.com/en-us/rest/api/machinelearning/webservices/createorupdate

以下代码运行一个成功返回的GET请求(200 OK和返回正文),但是对底部完全相同的uri的Put请求失败了400" Bad request"。

中间的代码基本上只是打开第一个请求的json输出,并试图在同一个uri上以PUT的输入格式发送相同的未更改数据。

任何人都可以帮我看看为什么会失败吗?

var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
HttpResponseMessage response = await httpClient.GetAsync($"https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroup}/providers/Microsoft.MachineLearning/webServices/{serviceName}?api-version=2016-05-01-preview");
var json = await response.Content.ReadAsStringAsync();
var f = JsonConvert.DeserializeObject<WebService>(json);

var requestBody = new RequestBody()
{
    location = f.Location,
    name = f.Name,
    tags = f.Tags,
    properties = f.Properties
};

var jsoncontent = JsonConvert.SerializeObject(requestBody, Formatting.None);
var content = new StringContent(jsoncontent, Encoding.UTF8, "application/json"); //tried simpler things like "{\"location\":\"West Europe\"}" with no result
var response2 = await httpClient.PutAsync($"https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroup}/providers/Microsoft.MachineLearning/webServices/{serviceName}?api-version=2016-05-01-preview", content, new JsonMediaTypeFormatter());
// last line returns a 400 bad request only.

然后我使用这个自定义对象;

private class RequestBody
{
    public string location { get; set; }
    public string name { get; set; }
    public WebServiceProperties properties { get; set; }
    public IDictionary<string, string> tags { get; set; }
}

1 个答案:

答案 0 :(得分:0)

这似乎是那些显而易见的事情之一。

&#34;,新的JsonMediaTypeFormatter()&#34;最后的片段破坏了它。删除它修复了问题,它没有它就返回200 OK。