Azure Web App的调用rest api时出现400-BadRequest [英] 400-BadRequest while call rest api of azure Web App
本文介绍了Azure Web App的调用rest api时出现400-BadRequest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
var client = new HttpClient();
client.DefaultRequestHeaders.Add("x-ms-version", "2016-05-31");
var content = new FormUrlEncodedContent(new KeyValuePair<string, string>[]
{
new KeyValuePair<string, string>("api-version", "2016-08-01")
});
content.Headers.ContentType = new MediaTypeHeaderValue("application/xml");
var response = client.PostAsync("https://management.azure.com/subscriptions/SuscriptionID/resourceGroups/Default-Web-SoutheastAsia/providers/Microsoft.Web/sites/MyAppName/stop?", content);
这是我对Azure WebApp rest api进行调用的方式,但我得到的状态码为:BadRequest
This is how I make a call to Azure WebApp rest api but I am getting statuscode : BadRequest
推荐答案
您的代码存在一些问题:
There are some issues with your code:
- 您正在混合
Azure服务管理API
和Azure资源管理器(ARM)API
。停止Web应用程序的API是Resource Manager API,因此您无需提供x-ms-version
。 - 您您的请求中缺少
Authorization
标头。 ARM API请求需要授权标头。请参阅此链接以了解如何执行ARM API请求: https:// docs .microsoft.com / en-us / rest / api / gettingstarted / 。
- You're mixing
Azure Service Management API
withAzure Resource Manager (ARM) API
. API to stop a web app is a Resource Manager API thus you don't need to providex-ms-version
. - You're missing the
Authorization
header in your request. ARM API requests require an authorization header. Please see this link on how to perform ARM API requests: https://docs.microsoft.com/en-us/rest/api/gettingstarted/.
基于这些,我修改了代码:
Based on these, I modified your code:
static async void StopWebApp()
{
var subscriptionId = "<your subscription id>";
var resourceGroupName = "<your resource group name>";
var webAppName = "<your web app name>";
var token = "<bearer token>";
var url = string.Format("https://management.azure.com/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Web/sites/{2}/stop?api-version=2016-08-01", subscriptionId, resourceGroupName, webAppName);
var client = new HttpClient();
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);
var t = await client.PostAsync(url, null);
var response = t.StatusCode;
Console.WriteLine(t.StatusCode);
}
请尝试使用此代码。假设您已经获得了正确的令牌,那么代码应该可以工作。
Please try using this code. Assuming you have acquired proper token, the code should work.
这篇关于Azure Web App的调用rest api时出现400-BadRequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文