I have the following webapi call
[HttpPost]
[Route("whatever")]
public async Task<IHttpActionResult> Apply([FromBody]string application)
application is an xml as string
How do I send it in postman?
In postman it works if I set it to body
the header I set it to content-Type - application/xml
But this only works if the parameter in the controller method is set to XElement.
As you can see above mine is a string, and this is always null.
Can I set a string as variable with inside as xml?
or How do pass an xml as string in postman ?
Many thanks
Related
I'm using Spring for my backend. I have the following code:
#RequestMapping(value = "/test", method = RequestMethod.POST)
#CrossOrigin
public void test(HttpServletRequest request) {
System.out.println(request.getParameterMap().size());
System.out.println(new JSONObject(request.getParameterMap()));
}
When I send JSON data using Postman, I get a map of all the parameters I've sent.
But when I'm making the same call from my website, I get an empty map with size 0. I do not get any error or exception on both front and back sides.
What could be the reason?
Thank you
Most probably getParameterMap() is really empty in your case, because parameters are not passed as query, but as a body (content) of HTTP request when it is sent from your web-site.
It can also be affected by the Content-Type and Accept headers of the HTTP request.
According to official documentation ServletRequest.getParameterMap() returns:
For HTTP servlets, parameters are contained in the query string or posted form data.
Usually "posted form data" implies: HTTP header Content-Type: application/x-www-form-urlencoded and URL encoded name-value pairs of parameters in the content of HTTP request.
If your web-site sends application/json, any other content type, or does not define content type at all, it might not be properly mapped into the request parameters by servlet container. In this case you should look into the body of HTTP request (ServletRequest.getReader()) to get the payload, or let Spring MVC do that (e.g. #RequestBody annotation).
Hi every one I am new to Asp.net Web API and I had my question post data using asp.net Web API and got my answer accepted.
This is an extension to the same question I want to post data with some header value in the Postman and my code is as follows
public HttpResponseMessage PostCustomer([FromBody] NewUser userData, string devideId)
{
//My code
return response;
}
When I hit this in Postman passing values in JSON format in BODY - raw I got message as follows
No HTTP resource was found that matches the request URI
No action was found on the controller that matches the request.
Please help me.
It looks like you have added some additional devideId string parameter to your action. Make sure that you are supplying a value to it as a query string when making the request:
POST http://localhost:58626/api/customers?devideId=foo_bar
If you don't want to make this parameter required then you should make it optional (in terms of optional method parameter in .NET):
public HttpResponseMessage PostCustomer([FromBody] NewUser userData, string devideId = null)
{
...
}
Now you can POST to http://localhost:58626/api/customers without providing a value for this parameter.
Remark: You don't need to decorate a complex object type (such as NewUser) with the [FromBody] attribute. That's the default behavior in Web API.
UPDATE: Here's how you could read a custom header:
public HttpResponseMessage PostCustomer(NewUser userData)
{
IEnumerable<string> values;
if (this.Request.Headers.TryGetValues("X-MyHeader", out values))
{
string headerValue = values.FirstOrDefault();
}
...
return response;
}
I am using web api with identity 2.0.
I want to assign multiple roles to a user.
I am using post man client to communicate with web api.
In api the method i used to get user id and roles for the user is given below:
public async Task<IHttpActionResult> AddRoleToUser(string userid,[FromUri] string[] selectedRoles)
Here selectedRoles is an array.
From postman client i am passing array as given below :
I am passing user id in the url.
Am i passing the array of roles in correct format from postman client?
The call to api was success ,but selectedRoles always contains null value.
I tried with raw json as given below,but it did not worked
If i can pass the roles as raw json,can any show an example
First problem: you're specifying that selectedRoles array comes from the URI (URL) with this attribute: [FromUri]. You need to remove this attribute, because you're sending it in the request body, not in the URL.
Web API actions can only receive a single parameter from the request body, and any number of parameters from the URL. So you need to pass the userid parameter as a query string parameter, like this ?userid=123123, and the other parameter in the body. (You could also create a route that includes a userid, or receive the userid as the id parameter and pass it as an URL segment, if you're using the deafult route)
You also need to specify in your headers the format of the information you're sending in the body. So you need to include this header: Content-Type: application/json, because you're including JSON in your request body.
Finally, as you're sending a single parameter from the body, you can send it like this:
['Admin', 'Employee']
If you want to use the format in your example, you shuould create a class to use as parameter in your action, that would look like this:
public class RoleList
{
public string[] selectedRoles { get; set; }
}
And your action should include this as parameter:
public async Task<IHttpActionResult>
AddRoleToUser(string userid, RoleList roleList)
method code in C#:
public void Put([FromBody]string[] selectedRoles)
{
foreach (var role in selectedRoles)
{
}
}
to call method in postman
in heading tab add your parameter and Content-type parameter with value application/x-www-form-urlencoded
in body tab select raw and JSON(application/json)
then in body box add your array like below:
['Admin', 'Employee']
I am using Spring MVC.
I am sending a POST request from an Ajax context and I sent one parameter. The Content-Type of the request is {'Content-Type':'application/json;'} and in Firefox's Firebug I see the post request is sent with one parameter:
JSON
orderId "1"
Source
{"orderId":"1"}
How can I get it in my controller in the server? The signature of my method is:
#ResponseBody
#RequestMapping(value = "/blahblah", method = RequestMethod.POST)
public void blahblah(#RequestBody(required=false) String orderId ){...
The incoming orderId is always null. Any ideas?
Thanks..
UPDATED: I used {'Content-Type':'application/x-www-form-urlencoded;'} and send my parameters in the following format "orderId=" + id . Also, I changed the server method using #RequestParam String orderId and the value is passed.
I used {'Content-Type':'application/x-www-form-urlencoded;'} and send my parameters in the following format "orderId=" + id .
I changed also the server method using #RequestParam String orderId and the value is passed.
I've confirmed I have the dropdown set to post and the URL is correct, because the string gets passed into my Web API project without error. However it is cutting everything off of the string after the first parameter
Request Headers:
User-Agent: Fiddler
Host: localhost:52888
Content-Length: 35
Content-Type: application/x-www-form-urlencoded
Request Body:
=name=TestName&date=10/15/2014
In the Web API project the only part that is passed is name=Test Name
I'm confident the query string is in the right format. I'm wondering if anyone else can point me in the direction of what I might be missing.
If I remove the = in front of the request body then nothing is received
A signature like AddCourse([FromBody] string courseRequest) tells WebAPI to look for a POST parameter named courseRequest. But (when it is properly formatted) your request body doesn't have that parameter - instead it has name and date. When you misformat the request body by prepending an = character, it apparently causes the parser to decide that name=test is the value. But the second part of the query string is after an &, and is clearly a different parameter. It has nowhere to bind that parameter, so it just gets dropped.
There are at least two solutions here. One would be to pass the parameters on the query string instead of in the request body, and use a method signature like: AddCourse(string name, string date) (note removed of [FromBody]).
Another would be to create a model object that encapsulates the request, something like
public class AddCourseModel{
public string Name {get;set;}
public string Date {get;set;}
}
and use that as the argument to your method: AddCourse([FromBody] AddCourseModel model).