从401未经授权的响应中提取自定义消息。

huangapple go评论99阅读模式
英文:

Extract custom message from 401 Unauthorized response

问题

我如何从API 401响应中提取我的自定义消息?

我在API中返回未经授权的消息:

  1. return Unauthorized("我的自定义消息");

在我的客户端Winform应用程序中,我想显示我的自定义消息,但是我从API获取的消息是"响应状态代码不指示成功:401(未经授权)"。

是否可能提取我的消息?

英文:

How do I extract my custom message from the API 401 response?

I return Unauthorized in the API:

  1. return Unauthorized("my custom message");

In my client winform app I want to show my custom message, but the message I get from the API is "Response status code does not indicate success: 401 (Unauthorized)."

Is it possible to extract my message?

答案1

得分: 0

使用类似以下示例的内容:

  1. // HttpClient is intended to be instantiated once per application, rather than per-use. See Remarks.
  2. static readonly HttpClient client = new HttpClient();
  3. static async Task Main()
  4. {
  5. // Call asynchronous network methods in a try/catch block to handle exceptions.
  6. try
  7. {
  8. using HttpResponseMessage response = await client.GetAsync("http://www.contoso.com/");
  9. response.EnsureSuccessStatusCode();
  10. string responseBody = await response.Content.ReadAsStringAsync();
  11. // Above three lines can be replaced with new helper method below
  12. // string responseBody = await client.GetStringAsync(uri);
  13. Console.WriteLine(responseBody);
  14. }
  15. catch (HttpRequestException e)
  16. {
  17. Console.WriteLine("\nException Caught!");
  18. Console.WriteLine("Message: {0}", e.Message);
  19. }
  20. }

你可以从响应体中提取你的消息:

  1. // 从响应体中提取你的消息
  2. string responseBody = response.Content.ReadAsStringAsync().Result;
英文:

Using something like this example: HttpResponseMessage Class

  1. // HttpClient is intended to be instantiated once per application, rather than per-use. See Remarks.
  2. static readonly HttpClient client = new HttpClient();
  3. static async Task Main()
  4. {
  5. // Call asynchronous network methods in a try/catch block to handle exceptions.
  6. try
  7. {
  8. using HttpResponseMessage response = await client.GetAsync("http://www.contoso.com/");
  9. response.EnsureSuccessStatusCode();
  10. string responseBody = await response.Content.ReadAsStringAsync();
  11. // Above three lines can be replaced with new helper method below
  12. // string responseBody = await client.GetStringAsync(uri);
  13. Console.WriteLine(responseBody);
  14. }
  15. catch (HttpRequestException e)
  16. {
  17. Console.WriteLine("\nException Caught!");
  18. Console.WriteLine("Message :{0} ", e.Message);
  19. }
  20. }

Yoou can extract your message from the response body:

  1. // Extract your message from the response body
  2. string responseBody = response.Content.ReadAsStringAsync().Result;

答案2

得分: 0

这是我做的事情 -

  1. var response = httpClient.PostAsync(url, hc).Result;
  2. string responseBody = response.Content.ReadAsStringAsync().Result;
  3. if (response.StatusCode != HttpStatusCode.OK)
  4. {
  5. throw new Exception(response.StatusCode + " - " + responseBody);
  6. }

现在我从API获得了自定义错误。

英文:

This is what I did -

  1. var response = httpClient.PostAsync(url, hc).Result;
  2. string responseBody = response.Content.ReadAsStringAsync().Result;
  3. if (response.StatusCode != HttpStatusCode.OK)
  4. {
  5. throw new Exception(response.StatusCode + " - " + responseBody);
  6. }

Now I get my custom error from the API.

huangapple
  • 本文由 发表于 2023年6月19日 12:58:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76503706.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定