how to run .NetCore Web API 6 without swagger

Saadia

Member
Joined
Dec 2, 2022
Messages
11
Programming Experience
1-3
hello

I need to build an API to login which accepts email and password with request type POST return an authentication token used for API authentication in header by passing barer token
and also return logged In user detail without password field.

What I have done so far is
built an API which is running swagger just showing response.

1672658205519.png



but now I am clueless what to do and how.
Secondly, it is deployed to Linux and there it is not showing error.

1672658529753.png
 
Well, presumably, the next step is to return an object that has the user details without the password. You'll need to look up the user details from your user list/repository/active directory.

You would turn also set a header value in the response that would have the token.

Show us your code. Post it as text in code tags, not as a screenshot. Tell us what you have tried. Tell us what you have researched.

As for your issue with deploying to Linux, what steps did you follow to deploy your app?
 
Well, presumably, the next step is to return an object that has the user details without the password. You'll need to look up the user details from your user list/repository/active directory.

You would turn also set a header value in the response that would have the token.

Show us your code. Post it as text in code tags, not as a screenshot. Tell us what you have tried. Tell us what you have researched.

As for your issue with deploying to Linux, what steps did you follow to deploy your app?
As I am working on it now the status is,
the Project is working fine in debugging mode as in swagger shows user details as well. But I want to run in a browser that's not working yet now.

1672673873501.png
 
I don't understand. Swagger is accessed via a browser. Your screenshot looks like it you are running in a browser.

I have a feeling that you are just trying to put in that URL in your browser. Chances are to that your API responds to a POST request. Most browsers will do a GET request when you put in a URL into the address bar.
 
I don't understand. Swagger is accessed via a browser. Your screenshot looks like it you are running in a browser.

I have a feeling that you are just trying to put in that URL in your browser. Chances are to that your API responds to a POST request. Most browsers will do a GET request when you put in a URL into the address bar.
yes I want to to access through its url ,as I am returning message , status and user details that should not be shown via api url?
 
And how exactly do you plan on getting your web browser to do a HTTP POST by just entering a URL into the address bar?

A web services API is different from a web form. Were you hoping that by putting in the URL you would get a form where you put in the user name and password and press enter? Unfortunately that's not how things work. You'll have to create a web page that has a form on it. You'll have to configure the form to do a POST with the input values from the form to to your web API URL.
 
You can see in the first screenshot that curl is doing a POST

If you have a code like:

C#:
class SomeController{
    [HttpPost("/api/what/ever")]
    string SayHello(){
      return "hello";
    }
}

And you put http://somehost/api/what/ever into a browser address bar, then as SD says, the browser will do a GET but the API is wired up to look for a POST - there isn't a GET endpoint so it give 404 (not found)

Either change your code to be [HttpGet] wired (typing into an address bar always does a GET) or change the way you call the API (use a <form method="post"... html, or javascript fetch('http://somehost/api/what/ever', { method: 'POST', ... etc)
 
You can see in the first screenshot that curl is doing a POST

If you have a code like:

C#:
class SomeController{
    [HttpPost("/api/what/ever")]
    string SayHello(){
      return "hello";
    }
}

And you put http://somehost/api/what/ever into a browser address bar, then as SD says, the browser will do a GET but the API is wired up to look for a POST - there isn't a GET endpoint so it give 404 (not found)

Either change your code to be [HttpGet] wired (typing into an address bar always does a GET) or change the way you call the API (use a <form method="post"... html, or javascript fetch('http://somehost/api/what/ever', { method: 'POST', ... etc)
Is it possible to display response in browser.
and please guide how authentication token could be added in header.
 
Is it possible to display response in browser.
Yes. Whatever your method returns is displayed in the browser.

I highly recommend taking a step back and understanding how HTTP works. You'll likely find several articles about this because this is a common interview question. This will give you more clarity on what a web API can or cannot do.
 
Yes. Whatever your method returns is displayed in the browser.

I highly recommend taking a step back and understanding how HTTP works. You'll likely find several articles about this because this is a common interview question. This will give you more clarity on what a web API can or cannot do.
Post method returning some response, but while url is being tried to open it returns nothing. but this error,

This page isn’t working​

If the problem continues, contact the site owner.


HTTP ERROR 405
 
How did you get your browser to do a POST?
 
Whatever your browser is doing, it's getting an HTTP405 ,which is "Method not allowed" if your browser is POSTing and POST isn't allowed, that's a problem - I'd check your config of your web server, and its logs to see what your browser is submitting, or I would open the developer tools of the browser, to see what is sent

Also, I'd use Insomnia* to test an API, at least initially, not a browser


*I used to recommend Postman, but then I realized it's horrible
 
Most people start out using Postman because it was the first tool around. But newer tools have come along that make testing web APIs easier.
 
Most people start out using Postman because it was the first tool around. But newer tools have come along that make testing web APIs easier.
Can you mention some of the newer tools that have come along that make testing Web API's easier or this not the topic for that?
 

Latest posts

Back
Top Bottom