http post method passing null values to the server
you should try below code its running very well for me.
// ADD YOUR REQUEST DATA HERE (you can pass number of variable).
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("Your_var_1", value));
nameValuePairs.add(new BasicNameValuePair("Your_var_2", value));
Now establish your web connection like
(1) Sending simple string to server
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("your url only ex:www.google.com/abc");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpParams httpParameters = new BasicHttpParams();
DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e)
{
Log.e("Loading Runnable Error in http connection :", e.toString());
}
(2) Send JSON Encode string to server
HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit
HttpResponse response;
JSONObject json = new JSONObject();
try {
HttpPost post = new HttpPost(URL);
json.put("user_name", "chintan");
json.put("password", "khetiya");
StringEntity se = new StringEntity( json.toString());
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
post.setEntity(se);
response = client.execute(post);
/*Checking response */
if(response!=null){
is = response.getEntity().getContent(); //Get the data in the entity
}
} catch(Exception e) {
e.printStackTrace();
createDialog("Error", "Cannot Estabilish Connection");
}
Response will same in both case
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
is.close();
result = sb.toString();
}
catch (Exception e)
{
Log.e("Loading Runnable Error converting result :", e.toString());
}
Now at the end result contain whole output string now its depend on you how you will read your data. using json or else. i am doing using json so put example code of it may be helpful to you.
JSONObject json_data = new JSONObject(result);// its a string var which contain output.
my_output_one = json_data.getString("var_1"); // its your response var form web.
my_output_two = json_data.getString("var_2");
Now its over you have two variable which having any kind of value and use any were.
Now this will helpful to you. if you have any query let me know.
Null value when Pass values [FromBody] to post method by Postman plugin
You can't bind a single primitive string using json and FromBody, json will transmit an object and the controller will expect an complex object (model) in turn. If you want to only send a single string then use url encoding.
On your header set
Content-Type: application/x-www-form-urlencoded
The body of the POST request message body should be =saeed
(based on your test value) and nothing else. For unknown/variable strings you have to URL encode the value so that way you do not accidentally escape with an input character.
Alternate 1
Create a model and use that instead.
Message body value: {"name":"saeee"}
c#
public class CustomModel {
public string Name {get;set;}
}
Controller Method
public HttpResponseMessage Post([FromBody]CustomModel model)
Alternate 2
Pass primitive strings to your post using the URI instead of the message body.
Passing Null as a Parameter to Controller POST Method
You can use an action filter to intercept the bad request.
See the question, Validating parameters in ASP.NET Web API model binder
Values null when I make a simple post request to my .net controller from angular
At your angular side update your method like this
updateMessage(message: any) {
console.log("at service")
console.log(message)
var newMessage = new CorpNotes(message['departments'], message['noteBody'], message['weeks'].weekEnding)
var Params = '?Department=' + message['departments'] + '&Note=' + message['noteBody'] + '&WeekEnding=' + message['weeks'].weekEnding
console.log(newMessage)
console.log(JSON.stringify(newMessage))
console.log(Params)
var item = {
"Departments": message["Departments"],
"Note": message["noteBody"],
"WeekEnding": message["weeks"]
}
return this.http.post(this.baseUrl + 'api/SlgCorpNotes/Edit', item).subscribe(res
=> {
console.log(res);
}, error => {
console.log(error);
});
}
Post parameter is always null
Since you have only one parameter, you could try decorating it with the [FromBody]
attribute, or change the method to accept a DTO with value as a property, as I suggested here: MVC4 RC WebApi parameter binding
UPDATE: The official ASP.NET site was updated today with an excellent explanation: https://learn.microsoft.com/en-us/aspnet/web-api/overview/advanced/sending-html-form-data-part-1
In a nutshell, when sending a single simple type in the body, send just the value prefixed with an equal sign (=), e.g. body:
=test
Pass null value to ASP.NET web service using HTTP POST?
Neither of these can have null values. They are both value types and can't be set to null even if you attempted to do it using code. I would recommend sending DateTime.MinValue
and 0.
Related Topics
Display a Part of the Webpage on the Webview Android
@Android Display /Res/Viewable in Webview
How to Call One Android Application from Another Android Application
Notifydatasetchanged Not Working on Recyclerview
How to Change Images on Imageview After Some Interval
How to Show Daily Offline Notifications in Android 10
How to Solve Nullpointerexception Error in Android
Custom Translucent Android Actionbar
Android Navigation Drawer on Top Actionbar
The Key Must Be an Application-Specific Resource Id
Display Fb Profile Pic in Circular Image View in Application
See Android Recent Task Executed by the User
Android Emulator Not Receiving Push Notifications
How to Create and Save a Screenshot from a Surfaceview
My App Was Rejected by Instagram Basic Display API Review Due to Invalid Reasons