1

I'm trying to pass data in POST request. In swagger, which runs automatically, they are passed, if you use POSTMAN or other client parameters are not passed. Thank you in advance for your help

[ApiController] public class UsersController { DatabaseContext context = new DatabaseContext(); [Route("loginUser")] [HttpPost] public string LoginUser(string email, string password) { # some logic } } 
7
  • How are you passing your params using postman? What is the endpoint you are calling? What is the response that your receive back? Commented Feb 5, 2022 at 6:53
  • I pass the parameters to the body, content-type = application/json. The logic is to return a certain string for invalid parameters and another string for correct parameters. Since there are no parameters, an error string is returned to the client, but swagger gets the correct response Commented Feb 5, 2022 at 7:18
  • Use the url generated by swagger in postman. Commented Feb 5, 2022 at 7:58
  • it didn't work :) Commented Feb 5, 2022 at 10:21
  • @politebarista You have to show us how you are sending the data to your API through postman? Did you select the raw option under the Body tab and Posted your JSON body to your API? Commented Feb 5, 2022 at 12:55

2 Answers 2

2

You can convert your parameters for your API to a strongly defined Model:

public class MyData { public string email {get;set;} public string password {get;set;} } 

And then you can use the [FromBody] attribute to get your values in your API method:

[ApiController] public class UsersController { DatabaseContext context = new DatabaseContext(); [Route("loginUser")] [HttpPost] public string LoginUser([FromBody]MyData data) { string email=data.email; string password=data.password; # some logic } } 
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks, I think this is a good solution, but I would like to understand why parameters are passed from swagger and not from third-party clients.
@politebarista There could reasons why postman is not sending the request correctly to your api. It could be a SSL problem as discussed here: stackoverflow.com/questions/66474715/… . Did you try the above method and check if your api is getting correct data from postman?
@politebarista Did you figure this one out?
not yet, no time for a pet-project right now. As soon as I try your method, I will definitely reply, but for now, I think you are right about the certificates
OK, I thought about your answer and remembered the following things: on the same server there is a GET request, which is accessed absolutely correctly, the expected parameters are returned; if it was about a certificate, the client simply could not contact the server, but the request is made, and the answer is returned, notifying that the user was not found
|
0

We need to pass the data using params section from postman screenshot

4 Comments

How will the client pass them on? I use the body in the current situation
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
It is a Post method. You need to define the body and post it to the api.
@politebarista the client can pass them as query params. To send using body create one complex object with username or password, we can only pass one param using body

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.