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).
Related
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
I am using Volley for my HTTP requests and I have an HTTP put URL which looks like below.
http://mycompany.com/favorite/{roomNumber}/count. I am using a JSON object request. How do I make the API work with the extra "/count" in the API? I am passing the parameter room number in the JSON object.
JSON Object request works fine with this type of URL "http://mycompany.com/favorite/{roomNumber}"
JSON Object request
JsonObjectRequest request = new JsonObjectRequest(METHOD_TYPE_PUT, url, jsonObjectParams, responseListener, errorListener)
Can somebody help me with passing the JSON object parameter in the middle of the URL
Thanks.
You can call the API dynamically like this,
private void getTheApiData(int roomNumber){
JsonObjectRequest request = new JsonObjectRequest(METHOD_TYPE_PUT,
"mycompany.com/favorite" + roomNumber + "/count",
jsonObjectParams, responseListener, errorListener)
}
and call the above API dynamically by the method when you get the new data every time like this.
getTheAPiData(20) //if room number is 20
let me know if you have any issue #Shravani
We had a spring web-app that used to handle all front and back end logic. As we need to scale we have split that into two microservices. How would I go about 'forwarding' a post request to another url (including its body and authentication headers). For example:
microservice1 has an endpoint /api/doSomething
microservice2 has an endpoint /privateUrl/doSomething
I want the user to hit the endpoint on microservice1 which will post to microservice2 and return the result.
I have tried with RestTemplate without much luck, i keep getting error "Could not write JSON: No serializer found for class..." from microservice1, i suspect this is because microservice1 doesnt know how to parse the body object microservice2 requires.
microservice1:
#PostMapping("/api/DoSomething")
fun postIT(request: HttpServletRequest, #PathVariable one: String, #PathVariable two: String){ ...}
microservice2:
#CrossOrigin
#PostMapping("/privateUrl/doSomething")
fun postIT(request: HttpServletRequest, #RequestParam one: String, #RequestParam(required = false, defaultValue = "true") two: String,#RequestBody it: IT) { ... }
I know i can parse the entire request in microservice1 and then send it to microservice2, however is there a way to just 'forward' the http request to a new url?
I am not sure what you mean , but if you want to communicate from one server to another you can always use rest template (or any other) and send http request from one service to another, you can see examples here
https://www.tutorialspoint.com/spring_boot/spring_boot_rest_template.htm
https://spring.io/guides/tutorials/bookmarks/
take a look it should work for you.
Can we have multiple content-type in Spring MVC request header?
I'm passing:
{Content-type = application/json, text/plain}
through Postman to my API. Currently, I'm getting org.springframework.web.HttpMediaTypeNotSupportedException: Invalid mime type ....
I wanted to know, is there something with my input values, or we can't have multiple content-type in our header.
Controller:
#RequestMapping(value = "/addressees", produces = APPLICATION_JSON_UTF8_VALUE, method = GET)
Yes, spring mvc request mapping supports multiple consumes MIME type , sample looks like
#RequestMapping(value = "/something", method = PUT,
consumes = {APPLICATION_JSON_VALUE, APPLICATION_XML_VALUE},
produces = {APPLICATION_JSON_VALUE, APPLICATION_XML_VALUE})
public SomeObject updateSomeObject(SomeObject acct) {
return doStuff(acct);
}
Add consumes part in requestmapping like - consumes = {APPLICATION_JSON_VALUE, APPLICATION_XML_VALUE}
For know more, refer this link -
https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/RequestMapping.html
Your request header can have one content-type per request. You specify to the server what type of data are actually being sent.
Your server/API endpoint can support multiple.
So if your request specifies both application/json and text/plain at the same time, I believe it is a problem with your request.
Yes, RequestMapping.consumes accepts an array of Mime types
String[] consumes() default {};
Note that you have to use consumes to define the incoming MIME types. produces is for the outgoing type.
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.