Why I can't correctly call this API passing an object into the request body?

why i can't
why can't i see anything
can't i meaning
can't see meaning
why i can't see john cena
why i can't download
don t can t
why can't i see straight

I am pretty new in .NET and C# (I came from Java and Spring framework) and I am finding the following difficulties calling an API in the correct way.

I will try to explain my problem in details.

I have this API (defined into a project deployed into IIS. Note, this project contains also other APIs that I am calling without problem):

[HttpPost]
[Route("api/XXX/InviaAlProtocollo/{siglaIDUor}")]
public string InviaAlProtocollo(MailBuffer mailBuffer, string siglaIDUor)
{
    ..........................................................................
    DO SOMETHING
    ..........................................................................
}

As you can see it take 2 input parameters:

  • MailBuffer mailBuffer that should be into the request body.
  • siglaIDUor that is into the URI.

I have some problem trying to pass the first parameter.

NOTE: I can't change the code of this API because was made by someone else and it can have impact on other thing.

Into another project deployed elsewhere I am trying to call the previous API (from a controller method) in this way:

[SharePointContextWebAPIFilter]
[HttpGet]
[ActionName("InviaMailAlProtocollo")]
public IHttpActionResult InviaMailAlProtocollo(string siglaIdUor)
{

    Console.WriteLine("INTO InviaAlProtocollo()" + siglaIdUor);

    // Ignore self signed certificate of the called API:
    ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };

    // Create the byte array[]
    UTF8Encoding encoding = new UTF8Encoding();
    byte[] mailContent = encoding.GetBytes("TEST");

    // Create the MailBuffer object that have to be passed to the API into the request body:
    MailBuffer content = new MailBuffer();
    content.Nome = "blablabla";
    content.Buffer = mailContent;

    string jsonRequest = urlBaseProtocolloApi + "/api/XXX/InviaAlProtocollo/ABC123";

    // Setting my credentials:
    credCache.Add(new Uri(jsonRequest), "NTLM", myCreds);

    HttpWebRequest spRequest = (HttpWebRequest)HttpWebRequest.Create(jsonRequest);
    spRequest.Credentials = credCache;
    spRequest.UserAgent = "Mozilla/4.0+(compatible;+MSIE+5.01;+Windows+NT+5.0";
    spRequest.Method = "POST";
    spRequest.Accept = "application/json;odata=verbose";
    spRequest.ContentType = "application/json; charset=UTF-8";

    // Create and set the stream:
    spRequest.ContentLength = mailContent.Length;

    Stream newStream = spRequest.GetRequestStream();
    newStream.Write(mailContent, 0, mailContent.Length);

    newStream.Close();

    // Obtain the response from the API:
    HttpWebResponse endpointResponse = (HttpWebResponse)spRequest.GetResponse();

    string sResult;
    JArray jarray;

    // Parse the response:
    using (StreamReader sr = new StreamReader(endpointResponse.GetResponseStream()))
    {
        sResult = sr.ReadToEnd();

        jarray = JArray.Parse(sResult);
        //JObject jobj = JObject.Parse(sResult);
    }

    Console.WriteLine(jarray);

    return Ok(jarray);

}

The problem is that when this method call my API the received MailBuffer mailBuffer input parameter is null (I see it debuggin my API and calling it).

I suspect that the problem could be related to this code section of my call:

// Create and set the stream:
spRequest.ContentLength = mailContent.Length;

Stream newStream = spRequest.GetRequestStream();
newStream.Write(mailContent, 0, mailContent.Length);

newStream.Close();

Probably I am trying to attach the wrong thing into the body of my request (the byte[] mailContent instead of the entire MailBuffer content object).

NOTE: To perform this call I have to use HttpWebRequest.

So, what is wrong? What am I missing? How can I fix this issue putting the entire MailBuffer content object into the body request and allowing my called API to retrieve it as input parameter?

The other project should make sure that the request is made with properly formatted data expected by the other API.

Right now you are sending just the raw bytes of the test email in the body of the request

//...

// Create the byte array[]
UTF8Encoding encoding = new UTF8Encoding();
byte[] mailContent = encoding.GetBytes("TEST");

// Create the MailBuffer object that have to be passed to the API into the request body:
MailBuffer content = new MailBuffer();
content.Nome = "blablabla";
content.Buffer = mailContent;

//...

Stream newStream = spRequest.GetRequestStream();
newStream.Write(mailContent, 0, mailContent.Length); //<---HERE ONLY SENDING encoding.GetBytes("TEST")

while the other endpoint is expecting data that can be deserialized to MailBuffer

Here is the portion of code that should be refactored to send the correct data

//...

UTF8Encoding encoding = new UTF8Encoding();
// Create the MailBuffer object that have to be passed to the API into the request body:
var content = new MailBuffer() {
    Nome = "blablabla",
    Buffer = encoding.GetBytes("TEST")
};
//convert model to JSON using Json.Net
var jsonPayload = JsonConvert.SerializeObject(content);
byte[] mailContent = encoding.GetBytes(jsonPayload); //<---CORRECT CONTENT NOW

spRequest.ContentLength = mailContent.Length;

Stream newStream = spRequest.GetRequestStream();
newStream.Write(mailContent, 0, mailContent.Length);

//...

Finally I would suggest using the simpler API of HttpClient to make the request. That however would be entirely up to your preference.

Here is an example of same call using HttpClient

[SharePointContextWebAPIFilter]
[HttpGet]
[ActionName("InviaMailAlProtocollo")]
public async Task<IHttpActionResult> InviaMailAlProtocollo(string siglaIdUor) {
    Console.WriteLine("INTO InviaAlProtocollo()" + siglaIdUor);
    // Ignore self signed certificate of the called API:
    ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };

    string requestUri = urlBaseProtocolloApi + "/api/XXX/InviaAlProtocollo/" + siglaIdUor;
    // Setting my credentials:
    credCache.Add(new Uri(requestUri), "NTLM", myCreds);

    var handler = new HttpClientHandler {
        UseDefaultCredentials = true,
        Credentials = credCache
    };

    var client = new HttpClient(handler);
    client.DefaultRequestHeaders.Add("UserAgent", "Mozilla/4.0+(compatible;+MSIE+5.01;+Windows+NT+5.0");
    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json;odata=verbose"));

    // Create the MailBuffer object that have to be passed to the API into the request body:
    var buffer = new MailBuffer() {
        Nome = "blablabla",
        Buffer = Encoding.UTF8.GetBytes("TEST")
    };
    //convert model to JSON using Json.Net
    var jsonPayload = JsonConvert.SerializeObject(buffer);
    var mailContent = new StringContent(jsonPayload, Encoding.UTF8, "application/json");
    // Obtain the response from the API:
    var response = await client.PostAsync(requestUri, mailContent);
    if (response.IsSuccessStatusCode) {
        var jarray = await response.Content.ReadAsAsync<JArray>();
        Console.WriteLine(jarray);
        return Ok(jArray);
    }
    return BadRequest();
}

"Why can't I see?" or "Why I can't see?"?, The answer is, they're both correct. Just used in different situations. "Why can't I see?" is a question, asking for the reason the "asker" is not perceiving something​  If you can't access your email, or you can't send and receive messages with your @icloud.com email address, learn what to do. Check the Outbox for unsent email If you get a message that says your email wasn’t sent, then that email goes to your Outbox.

Use [FromBody] parameter.

[HttpPost]
[Route("api/XXX/InviaAlProtocollo/{siglaIDUor}")]
public string InviaAlProtocollo([FromBody]MailBuffer mailBuffer, string siglaIDUor)
{
    ..........................................................................
    DO SOMETHING
    ..........................................................................
}

Also try passing MailBuffer as JSON object, it will be automaticly converted to MailBuffer object when you pass this from body.

If this won't work switch MailBuffer object in method with similar object and then map this object to MailBuffer.

Why I can't watch the police videos anymore, Kevin B. Blackistone is an ESPN analyst and visiting professor at the Philip Merrill College of Journalism at the University of Maryland. Van Halen - Why Can't This Be Love (1986). Most popular single released from the album "5150" with new lead singer Sammy Hagar. Van Halen - Why Can't This Be Love Lyrics:

You can give a try with HttpClient (using System.Net.Http)

private static readonly HttpClient client = new HttpClient();

// Create the MailBuffer object that have to be passed to the API into the request body:
MailBuffer content = new MailBuffer();
content.Nome = "blablabla";
content.Buffer = mailContent;

var values = new Dictionary<string, object>
{
   { "mailBuffer", content },
   { "siglaIDUor", siglaIdUor }
};
var content = new FormUrlEncodedContent(values);
var response = await client.PostAsync("/api/XXX/InviaAlProtocollo/ABC123", content);

var responseString = await response.Content.ReadAsStringAsync();

Why I Can't Recommend the Keto Diet for Depression, Causes of insomnia: Figuring out why you can't sleep. In order to properly treat and cure your insomnia, you need to become a sleep detective. Emotional issues​  I can’t reset my Facebook password because I can't access the email or mobile phone number on my account. Here are some things you can try to get back into your account so you can reset your password.

Insomnia, Our beliefs and thoughts impact our lives more than any relationship.It would be wise to evaluate how we came to them and whether they truly  Note that you cannot repair a damaged PDF itself. However, if you have the source file, you can re-create the PDF using an Adobe product, such as Acrobat or Photoshop Elements. Most modern web browsers can easily open PDFs. If you’re having trouble opening a specific PDF on a web page, try the

Why I Can't.. or Why Can't I?, I could see looming on the horizon the ugly specter of a breakdown in negotiations among the nations we represented, triggering a series of reprisals and  We can't carry a faucet with us, and that's where hand sanitizer comes in, writes Ford Vox

Why I Can Believe In Modern Miracles, Quite frankly, I can think of no question more important to a writer and to a culture, especially since continued actions emerge from these imaginings, derived  There's a reason you can't get a refund from Ticketmaster for a postponed concert or Broadway show — the online ticket seller doesn't have your money. The venue does.

Comments
  • How does this question differ from the other 3 questions you've already posted on the same problem?
  • Why do you have to use HttpWebRequest to perform the call?
  • The Web API is not sending the data formatted to what the first API expects, so the model binder wont know how to populate the parameters. Check my submitted answer for more details.
  • NOTE: I can't change the code of this API because was made by someone else and it can have impact on other thing.