Home > Enterprise >  RestAPI C# and Python: Can't understand or solve status code 308
RestAPI C# and Python: Can't understand or solve status code 308

Time:11-23

I'm trying to learn RestApis and have reached a problem I cannot find the answer to.

In a Python script I run the RestService with the help of the Flask RestApi package.
All endpoints are GET and I have tested and verified each endpoint with PostMan and a web browser.
In each of those two clients I get the JSON-data as I expect.

I have a C# Console application running as the RestClient.
Here I get the status code 308 (Permanent Redirect).
I cannot understand why I get this status code.

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()

The RestService runs localy in a Windows Terminal on my computer.
The URL for the RestService is: http://localhost:5000/

My 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);
}

Both methods returns with server code 308 (Permanent Redirect).

The RestSharp returns with this information:

<!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.

I have found this explaination abouot status code 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

My questions:

  • How can the resourse have been "moved" to another URI?
  • Is the problem in the service or the client?
  • What do I need to do, to fix this?

CodePudding user response:

Try like this

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);

CodePudding user response:

I found the problem!
It was on the client's side.

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;
}

I was missing the last slash in the URL.
That caused the problem.

  • Related