0

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?
asked Nov 21, 2021 at 22:30

2 Answers 2

2

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.

answered Nov 22, 2021 at 23:56
Sign up to request clarification or add additional context in comments.

1 Comment

Thank for saving me.I was missing / at the end of the url. Without slash it was working fine in brower and postman but c# is demon. Wasted my precious hours of time.
1

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);
answered Nov 22, 2021 at 6:04

3 Comments

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
I tried your code but same result. I have no authentication what I'm aware of. What to do?
i got now . you are redirecting /name to main so u cant get or post that url .

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.