简体   繁体   中英

ASP.NET POST request to same host results in a null payload

I want to hit some ASP.NET APIs from the same application that hosts the APIs. The real world use case is that I want to upload a file to an MVC controller, parse it out and make a bunch of separate API calls via POST requests to APIs that are hosted on the same application. I can see that the appropriate API endpoints are getting hit by setting a breakpoint inside of the WebApi controller method, but the payload is always null. I've tried using Flurl, RestSharp, WebClient, and WebRequest to make the request, and none of them allow the payload to make it through. It also fails with the null payload problem if I try to invoke these POST requests via unit/integration tests.

Is there some sort of built in limitation to the ASP.NET framework that prevents a payload from being posted to the same application?

var myObjectRow = new JObject();
myObjectRow["Key"] = 1;


/* RestSharp */
var request = new RestRequest("/api/MyObject", Method.POST);
request.AddHeader("Content-type", "application/json; charset=utf-8");
request.AddBody(request.JsonSerializer.Serialize(myObjectRow));
request.AddJsonBody(myObjectRow);
request.AddParameter("Application/Json", myObjectRow, ParameterType.RequestBody);
var response = RestClient.Execute<MyObject>(request);


/* WebClient */
using (var webClient = new WebClient())
{
    webClient.Headers[HttpRequestHeader.ContentType] = "application/json";
    var payload = JsonConvert.SerializeObject(myObjectRow);
    var response = webClient.UploadString(BaseUrl + "/api/MyObject", "POST", payload);
}


/* WebRequest */
var payload = JsonConvert.SerializeObject(myObjectRow);
byte[] buf = Encoding.UTF8.GetBytes(payload);
var request = (HttpWebRequest)WebRequest.Create(BaseUrl + "/api/MyObject");
request.Method = "POST";
request.ContentLength = buf.Length;
request.ContentType = "application/json; charset=utf-8";
request.GetRequestStream().Write(buf, 0, buf.Length);

using (var response = (HttpWebResponse)request.GetResponse())
{
    var responseStream = response.GetResponseStream();
    var streamReader = new StreamReader(responseStream, Encoding.UTF8);
    var responseString = streamReader.ReadToEnd();
}

The target endpoint is something like this:

public class MyObjectController : ApiController
{
    public MyObject Post(MyObject myObject)
    {
        // myObject shows up null here
        return myObject;
    }
}

Try this:

 public class MyObjectController : ApiController
{
    public MyObject Post(MyObject myObject)
    {
         ModelState.Clear();
        // myObject shows up null here
        return myObject;
    }
}

I figured out the problem. The Excel reader I'm using was adding ".0" to integer fields and that was causing the deserialization to silently fail in the POST request handler. The payload makes it through without that incompatibility.

Try to rename myObjectRow to myObject. I believe it expects for parameters to be the same name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM