我正在嘗試學習 RestApis 并且遇到了一個我找不到答案的問題。
在 Python 腳本中,我在 Flask RestApi 包的幫助下運行 RestService。
所有端點都是 GET,我已經使用 PostMan 和 Web 瀏覽器測驗并驗證了每個端點。
在這兩個客戶端中的每一個中,我都按預期獲得了 JSON 資料。
我有一個 C# 控制臺應用程式作為 RestClient 運行。
在這里我得到狀態代碼 308(永久重定向)。
我不明白為什么我會得到這個狀態代碼。
Python RestService
from flask import Flask
from flask import jsonify
app = Flask(__name__)
@app.route('/name/')
def nameEndpoint():
return jsonify(
{
'name':"Some name"
}), 200
if __name__ == '__main__':
app.run()
RestService 在我的計算機上的 Windows 終端中本地運行。
RestService 的 URL 是:http://localhost:5000/
我的 C# RestClients
var url = "http://localhost:5000/name";
public async Task TryWithRestSharp()
{
var client = new RestSharp.RestClient(url);
var result = await client.ExecuteAsync(new RestSharp.RestRequest());
var json = foo.Content;
}
public async Task TryWithHttpClient()
{
var client = new HttpClient();
var json = await client.GetStringAsync(url);
}
這兩種方法都回傳服務器代碼 308(永久重定向)。
RestSharp 回傳此資訊:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>Redirecting...</title>
<h1>Redirecting...</h1>
<p>You should be redirected automatically to target URL:
<a href="http://localhost:5000/name/">http://localhost:5000/name/</a>.
If not click the link.
我發現這個關于狀態碼 308 的解釋:
A 308 Permanent Redirect message is an HTTP response status code indicating that the
requested resource has been permanently moved to another URI, as indicated by the
special Location header returned within the response
我的問題:
- 資源如何“移動”到另一個 URI?
- 是服務的問題還是客戶端的問題?
- 我需要做什么來解決這個問題?
uj5u.com熱心網友回復:
像這樣嘗試
https://restsharp.dev/getting-started/getting-started.html#basic-usage
using RestSharp;
using RestSharp.Authenticators;
var client = new RestClient("your base url");
// if there is any auth
client.Authenticator = new HttpBasicAuthenticator("username", "password");
var request = new RestRequest("url prefix like url/name", DataFormat.Json);
var response = client.Get(request);
uj5u.com熱心網友回復:
我發現了問題!
這是在客戶這邊。
using RestSharp; // NOTE: Not sure if this make any difference for the problem. But it nice to have.
var url = "http://localhost:5000/name/"; // NOTE: Make sure to end the URL with a slash.
public async Task TryWithRestSharp()
{
var client = new RestClient(url);
var request = new RestRequest(url, DataFormat.Json);
var result = await client.ExecuteGetAsync(new RestRequest());
var json = result.Content;
}
我錯過了 URL 中的最后一個斜杠。
這導致了問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/365862.html
標籤:Python C# 休息 http-status-code-308
上一篇:如何在python中的休息請求查詢中傳遞日期引數:sysparm_query
下一篇:將二進制檔案轉換為位元組陣列
