I have this method:
[HttpPost]
[Route("Post")]
public async Task<IHttpActionResult> Post(int adminTestId)
I am sending the following but it's giving me an error:
POST /api/UserTest/Post
body {"adminTestId":1197}
Can someone tell me how I can set the parameter to the method so it will accept adminTestId?
Here is the message that I am getting:
{"message":"No HTTP resource was found that matches the request URI 'http://localhost:3048/api/UserTest/Post'.","messageDetail":"No action was found on the controller 'UserTest' that matches the request."}
I believe you are setting the incorrect route:
[Route("UsetTest/Post")]
This is assuming api/ is the correct prefix.
In Web API, requests are mapped to actions based on HTTP verbs.
Therefore, you should not include Post in your URL which defeats the purpose of using Web API and violate REST rules.
Post Json
If you want to post json, you want to bind to a model.
Post Integer
If you just want to accept an single integer value, you just post an integer instead of json.
Related
I am creating an ASP.NET Core 6.0 Web API.
This is my controller
public virtual IActionResult GetStatus([FromHeader][Required()] string authorization, [FromRoute][Required] string requestId)
{ }
Problem is the request header is not accepting a parameter with name authorization. If I change name to something else it works fine. But when I change name to authorization, it is not working. I added a middleware and see that the request header does not have authorization in header.
My organization specification wants the name to be authorization only so I can not have any other name here.
Is there any way a Web API endpoint can accept a parameter named "authorization".
I tried creating a custom AuthenticationHandler, it does not solve the problem.
Is there any way a Web API endpoint can accept a parameter named
"authorization"
Reason: 1
Well, your issue is pretty obvious. You may hear about reserved keyword. Like in C# there are some compiler-reserve key for example static, class public and so on. We cannot set variable name as reserve key word.
If you navigate to System.Net.Http.Headers namespace and then move to below details, you would see Authorization is a reserve key word for HttpClientb request and its not wise to manipulate that system reserve word at all.
client.DefaultRequestHeaders.Authorization
Reason: 2
Likewise, if you use or attached Swagger in your program.cs so no longer you cannot use authorization, Content-Type, Accept as your self defined parameter, even if you use it will be skipped. However, Postman can allow you to do that. You can check swagger official reference here.
You can see the example below:
Swagger Request:
Postman Request:
Note:
Apparently, you can ommit swagger then it might work. Nonetheless, if you are using Authorization and Authentication within your application, then you might face unexpected issues if you still stick to use authorization as your custom parameter.
This is my case, set by step:
Run below page under address:
localhost:8080/user/data/expenses
On this page click link
localhost:8080/user/data/change_button/9_2015
Now my controller is started:
My question:
How can I get information from what side was call my controller. I need exactly this information which is strong below:
localhost:8080 /user/data/expenses
M-Z
Infact there is way to get the referer of the request as it is part of the HTTP header. Just inject HttpServletRequest to the mapped method then get the header named "referer".
request.getHeader("referer");
You need to inject the HttpServletRequest and then use getRequestURI() to get the data. Read the javadoc for #RequestMapping.
There is a initial request going to the server which should retrieve the CSRF token and use that token id in post request header.
if that does not happen any POST requests to the server will return that error.
In the above screen shot, where token is the request to get the CSRF token Id, If I run the test this will generate one dynamic random token ID. But I need to pass the generated token ID in the post request through Header Manager. How can it possible. If yes, Can any one suggest some way to do that.
I resolved it by using User defined variables and Regular Expression Extractor to pass the parameters from one request sampler to another.
In the firs request add a postprocesor of the request. if the response is un json format user json Extractor, in json Extractor define a variable that read the token
In the second request add a header Manager an refer varaible declare in json Extractor in the following way in value cell, ${variable}
I am writing a Web API to handle some request. The request url schema is fixed. I cannot modify it.
So I have to collect all the necessary info from places like:
query string
headers
cookie
web form post data
How can I access all these locations within a Web API action method?
The Query String and Post Data information can be received as Web API Method parameters (preferred way, since Web API will do the necessary binding for you)
Use the FromBody or FromUri attributes on the method parameters)
OR
you could access it using the old fashioned way of Request object. you have access to the http request using the Request object in your Web API Action method. you can get all the information using..
Request.QueryString
Request.Form["name"]
Request.Cookies
Request.Headers
I am calling webservice which takes two parameters like category and salt then provide the json output with constructed url but url is not working. PFA url
Service Url:
http://qalisting.corelogic.com/ChaseListingServices/v1.5/test
constructed URL: http://qalisting.corelogic.com/ChaseListingServices/v1.5/listings/search/test/0/4e9c00b32794edfeba257aa0c74f500b
Looking at the error message on your constructed URL, the Service needs to be called using a POST and not a GET Request.
Responding to your comments: it's not the URL which is the problem but how it is called. When you follow a link, such as the one you posted - the browser sends it as a GET request. The service is expecting the JSON arguments as part of the "body" of a POST request. This must be done programmatically.