Sumit raj
Sumit raj

Reputation: 831

Getting Post API Call argument as null

I am trying to make a post call with very large argument size. But the call is not going through.

Issue: When size of parameter is large call is not going through.But in case of small parameter everything is fine that means API is working. I tried to research about POST call but couldn't make it work.

What i Did I made a sample api project and tried it to test via postman and project too.

Postman call Postman call This is working fine as expected.

In-Project enter image description here enter image description here

If someone is going to point out rquest.ContentLength = 0;. This is because param is getting appended to url only. I don't intend it to get appended but i don't know how to do it and googled too. For small length this is working but say if string length goes to 3000 it fails.

API Call Initiator

UriBuilder uriBuilder = new UriBuilder(restURL);
var query = HttpUtility.ParseQueryString(uriBuilder.Query);
query["json"] = new JavaScriptSerializer().Serialize(reportParameterDictionary);
uriBuilder.Query = query.ToString();
restURL = uriBuilder.ToString();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(restURL);
request.Method = "Post";
request.ContentType = "application/json";
rquest.ContentLength = 0;
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
  StreamReader reader = new StreamReader(response.GetResponseStream());
  string responseData = reader.ReadToEnd();
  response.Close();
  dynamic obj = JsonConvert.DeserializeObject(responseData);
  if (obj != null)
  {
   printresult = obj.Success;
  }
}

Receiving End

[Route("PrintReport")]
public IHttpActionResult PrintReport(string json)

Upvotes: 0

Views: 1162

Answers (2)

artista_14
artista_14

Reputation: 716

// Create a request using a URL that can receive a post.   
WebRequest request = WebRequest.Create ("http://www.contoso.com/PostAccepter.aspx");  
// Set the Method property of the request to POST.  
request.Method = "POST";  
// Create POST data and convert it to a byte array.  
string postData = "This is a test that posts this string to a Web server.";  
byte[] byteArray = Encoding.UTF8.GetBytes (postData);  
// Set the ContentType property of the WebRequest.  
request.ContentType = "application/x-www-form-urlencoded";  
// Set the ContentLength property of the WebRequest.  
request.ContentLength = byteArray.Length;  
// Get the request stream.  
Stream dataStream = request.GetRequestStream ();  
// Write the data to the request stream.  
dataStream.Write (byteArray, 0, byteArray.Length);  
// Close the Stream object.  
dataStream.Close ();  
// Get the response.  
WebResponse response = request.GetResponse ();  
// Display the status.  
Console.WriteLine (((HttpWebResponse)response).StatusDescription);  
// Get the stream containing content returned by the server.  
dataStream = response.GetResponseStream ();  
// Open the stream using a StreamReader for easy access.  
StreamReader reader = new StreamReader (dataStream);  
// Read the content.  
string responseFromServer = reader.ReadToEnd ();  
// Display the content.  
Console.WriteLine (responseFromServer);  
// Clean up the streams.  
reader.Close ();  
dataStream.Close ();  
response.Close ();  

Follow this Link for complete explanation.

Upvotes: 2

Tanato
Tanato

Reputation: 1173

Have you tried to use HttpClient instead of HttpWebRequest?

var client = new HttpClient();
var content = new StringContent("YOUR LONG STRING", 
                             System.Text.Encoding.Unicode, 
                             "application/json");            
var task = client.PostAsync(restURL, content);
var str = await task.Result.Content.ReadAsStringAsync();   

Upvotes: 0

Related Questions