I'm new to web api. When I'm sending json string of 10MB, the method parameter is showing Null. if I reduce the json string size, then the parameter is showing the string, which I actually sending in Http body. When I googled, I found maxAllowedContentLength property's default value is 30000000 bytes. But my string size is far smaller than this value. Why my http post method is not taking large string as Parameter? How to solve this problem?
Instead of using Parameter for Post method, read input from http content as follows:
var msg = Request.Content.ReadAsStringAsync();
var msgResult = msg.Result;
string reqString = msgResult.ToString();
Related
I want use ActiveMQ in .net core,i use Apache.NMS.ActiveMQ for doing this but I have problem.
I see this error in ActiveMQ admin console:
Cannot display ObjectMessage body. Reason: Failed to build body from bytes. Reason: java.io.StreamCorruptedException: invalid stream header: 00010000
thats part of my code for doing this:
private const String QUEUE_DESTINATION = "test-queue";
private IConnection _connection;
private ISession _session;
public MessageQueue()
{
IConnectionFactory factory = new ConnectionFactory("tcp://localhost:61616?wireFormat.maxInactivityDuration=5000000");
_connection = factory.CreateConnection();
_connection.Start();
_session = _connection.CreateSession();
}
IDestination dest = _session.GetQueue(QUEUE_DESTINATION);
using (IMessageProducer producer = _session.CreateProducer(dest))
{
var objectMessage = producer.CreateObjectMessage(newDoc);
producer.Send(objectMessage);
}
The fact that the admin console can't display the body of an ObjectMessage isn't really an error. It's the expected behavior. Remember, from the broker's perspective the message body is just an array of bytes. It could be text data (encoded many different ways), image data, custom binary data, etc. The broker has no idea how to decode it. It will try to display the body as text, but if it fails it won't try anything else.
To be clear, in order to see the contents of an ObjectMessage the web console would have to have the object's definition in order to deserialize it. There is no mechanism to tell the web console about arbitrary data formats so it can deserialize message bodies reliably (other than simple text). This is one reason, among many, to avoid ObjectMessage.
I recommend you use a simple text format (e.g. JSON, XML) to represent your data and send that in your message rather than using ObjectMessage.
How to send a boolean value or an integer in http request body in dart? The documentation says that I can only send string, list or a Map. How to make it work for boolean or integer in Dart?
Try this approach which I use in my projects
void apiPost(){
var body = jsonEncode(
{
"string":"test",
"bool":true,
"int":1
});
http.post("url here",body: body,headers: {"Content-Type": "application/json"}).then((response) {
Map map = json.decode(response.body);
});
}
As you saw in the documentation, you can only send those three things: a byte array, a string or a map of string to string. In fact, in the end, you are just sending a byte array. The other options get converted to bytes before sending.
A string is converted to bytes by transforming it into bytes using utf-8 (or some other encoding specified by the content-type header).
A map of string to string is encoded to first as x-www-form-urlencoded and then to bytes. That encoding can, of course, only encode strings.
So the answer to your question really depends on where/how the server expects to receive the fields. If it's expecting them in a form, then it's your responsibility to convert, say, a boolean into whatever string the server expects. Maybe that's 'true'/'false' or '1'/'0' or something else. Make your map like this:
int someInt;
bool someBool;
var formData = <String, String>{
'an_int_value' : someInt.toString(),
'a_bool_value' : someBool.toString(), // assuming 'true'/'false' is OK
};
Also consider the possibility that your server requires a completely different encoding, like JSON. Then you would convert your map to JSON, then pass that as a string, setting the content-type appropriately.
What is the difference between HttpContext.Current.Request.Url.OriginalString and HttpContext.Current.Request.Url.AbsoluteUri?
I need to get the URL in the page load, but confused about the both.
Depending on the url, you could get the same result. It depends how exact you want it to be, if you want all fragments, and how you want to use it.
The following is for Uri but I think it applies to Url too (a url is a uri, essentially).
AbsoluteUri
The AbsoluteUri property includes the entire URI stored in the Uri instance, including all fragments and query strings.
This
Uri baseUri= new Uri("http://www.contoso.com");
Uri myUri = new Uri(baseUri,"catalog/shownew.htm?date=today");
Console.WriteLine(myUri.AbsoluteUri);
will return
http://www.contoso.com/catalog/shownew.htm?date=today
https://msdn.microsoft.com/en-us/library/system.uri.absoluteuri(v=vs.110).aspx
Uri.OriginalString
Gets the original URI string that was passed to the Uri constructor.
The following example creates a new Uri instance from a string. It illustrates the difference between the value returned from OriginalString, which returns the string that was passed to the constructor, and from a call to ToString, which returns the canonical form of the string.
// Create a new Uri from a string address.
Uri uriAddress = new Uri("HTTP://www.ConToso.com:80//thick%20and%20thin.htm");
// Write the new Uri to the console and note the difference in the two values.
// ToString() gives the canonical version. OriginalString gives the orginal
// string that was passed to the constructor.
// The following outputs "http://www.contoso.com/thick and thin.htm".
Console.WriteLine(uriAddress.ToString());
// The following outputs "HTTP://www.ConToso.com:80//thick%20and%20thin.htm".
Console.WriteLine(uriAddress.OriginalString);
https://msdn.microsoft.com/en-us/library/system.uri.originalstring(v=vs.110).aspx
An argument to a Spring MVC method can be declared as RequestBody or RequestParam. Is there a way to say, "Take this value from either the body, if provided, or the URL parameter, if not"? That is, give the user flexibility to pass it either way which is convenient for them.
You can make both variables and check them both for null later on in your code
like this :
#RequestMapping(value = GET_SOMETHING, params = {"page"}, method = RequestMethod.GET)
public
#ResponseBody
JSONObject getPromoByBusinessId(
#PathVariable("businessId") String businessId, #RequestParam("page") int page,
#RequestParam("valid") Boolean valid,
#RequestParam("q") String promoName) throws Exception {}
and then use a series if if-else to react to requests.
I wrote it to work with any of the three params be null or empty, react to all different scenarios.
To make them optional, see :
Spring Web MVC: Use same request mapping for request parameter and path variable
HttpServletRequest interface should help solve this problem
#RequestMapping(value="/getInfo",method=RequestMethod.POST)
#ResponseBody
public String getInfo(HttpServletRequest request) {
String name=request.getParameter("name");
return name;
}
Now, based on request data coming from body or parameter the value will be picked up
C:\Users\sushil
λ curl http://localhost:8080/getInfo?name=sushil-testing-parameter
sushil-testing-parameter
C:\Users\sushil
λ curl -d "name=sushil-testing-requestbody" http://localhost:8080/getInfo
sushil-testing-requestbody
C:\Users\sushil
λ
1)When exactly we need to use HTTPContext (exclusively), please elaborate with an example if possible.
2)What does the following code do
internal string QueryString( string name )
{
if ( HttpContext.Current.Request.QueryString[name] == null )
return string.Empty;
else {
return HttpContext.Current.Request.QueryString[name].ToString();
}
3)Is there any other alternative way where we avoid using HttpContext
Q1:
HttpContext is an object which encapsulates all HTTP-specific information about an individual HTTP request. So, if you need to get any information about receiving HTTP request (for example in order to get the query string parameter values, referral urls, User's IP address etc), you need to use HttpContext object. Further more, this object contains information of current Request, Response, Server, Session, Cache, User and etc.
More information
Usage and examples
Get current session Id: HttpContext.Session.SessionID
Get Timestamp of current request: HttpContext.Timestamp.ToString()
Q2:
In your code sample, you are trying get the value of query string parameter 'name'.
Your dummy request Url looks like - http://www.yourdomain.com/something?name=queryStringValue
QueryString parameter = name
QueryString parameter value = queryStringValue
Hope this helps.