Solution 1 :

There are several things to do or to check:

(1) Make all properties public in your class updateConsentObj.

(2) Make sure that your POST-request includes a header that says Content-Type: application/json, this tells MVC to use a JSON parser.

(3) There is a missing " in the JSON you posted, it should be {"consentInstanceId":"my-consent","ampUserId":"amp-xxxx","consentState":true,"consentStateValue":"accepted"}

With all the above applied, I was able to make a successful call to both types of Action Method. See below for the code I used and the results.

Using a class:

[HttpPost]
public ActionResult updateconsent([System.Web.Http.FromBody]updateConsentObj obj)
{
    return Content(
        "OK: " +
        "obj.consentInstanceId = [" + (obj.consentInstanceId ?? "(null)") + "], " +
        "obj.ampUserId = [" + (obj.ampUserId ?? "(null)") + "], " +
        "obj.consentState = [" + obj.consentState + "], " +
        "obj.consentStateValue = [" + (obj.consentStateValue ?? "(null)") + "]");
}

This produces

OK: obj.consentInstanceId = [my-consent], obj.ampUserId = [amp-xxxx], obj.consentState = [True], obj.consentStateValue = [accepted]

Using separate parameters:

[HttpPost]
public ActionResult updateconsent(string consentInstanceId, string ampUserId, bool consentState, string consentStateValue)
{
    return Content(
        "OK: " +
        "consentInstanceId = [" + (consentInstanceId ?? "(null)") + "], " +
        "ampUserId = [" + (ampUserId ?? "(null)") + "], " +
        "consentState = [" + consentState + "], " +
        "consentStateValue = [" + (consentStateValue ?? "(null)") + "]");
}

This produces

OK: consentInstanceId = [my-consent], ampUserId = [amp-xxxx], consentState = [True], consentStateValue = [accepted]

Solution 2 :

Add [ValidateInput(false)] attribute to your action.

This was because .NET detected something in the entered text that looked like an HTML statement. Therefore to disable request validation, you must add the [ValidateInput(false)] attribute to action.

For example:

If your model must be as below:

public class updateConsentObj
{
    public string consentInstanceId { get; set; }
    public string ampUserId { get; set; }
    public bool consentState { get; set; }
    public string consentStateValue { get; set; }
}

,your action must be as below:

[HttpPost]
[ValidateInput(false)]
public ActionResult updateconsent(updateConsentObj obj)
{
}

Problem :

We are using amp-html and amp-consent to managed cookie consents on our site. This has a setting which you can specify an end point for onUpdateHref

I can see this request is being made in the network tab, and that there is a request payload.

{"consentInstanceId":"my-consent","ampUserId":"amp-xxxx","consentState":true,"consentStateValue":"accepted"}

I am struggling to work out the MVC action method signature to receive this request.
I have tried adding this as individual properties in the method:

[HttpPost]
public ActionResult updateconsent(string consentInstanceId, string ampUserId, bool consentState, string consentStateValue)
{
}

Or creating a class definition:

public class updateConsentObj
{
    string consentInstanceId { get; set; }
    string ampUserId { get; set; }
    public bool consentState { get; set; }
    public string consentStateValue { get; set; }
}

And passing this as the property:

public ActionResult updateconsent(updateConsentObj obj)
{
}

I also tried adding FromBody to the signature:

public ActionResult updateconsent([System.Web.Http.FromBody]updateConsentObj obj)
{
}

Everything I try results in null values.

The only way I can read the payload data is the following:

Stream req = Request.InputStream;
req.Seek(0, System.IO.SeekOrigin.Begin);
string json = new StreamReader(req).ReadToEnd();

enter image description here

Comments

Comment posted by Peter B

You need to make

Comment posted by mp3duck

Good spot on not all properties being public but unfortunately that didn’t resolve the issue. To be clear, obj is not full, but it’s nulable properties (i.e. the strings) are.

Comment posted by mp3duck

Thanks for the reply Peter. I am not able to control the headers in the request, as it’s being made by the amp-consent control. Point 3 was just a typo, which I have now corrected. I made all properties public, and it still doesn’t work.

Comment posted by mp3duck

I’ve added a screenshot of the request that I am seeing in the network tab. (actually I added the wrong event. correcting now)

Comment posted by Peter B

That sounds like a possible bug or deficiency in that library. It would be best to get them to fix it, or at least to look for a way to make it send data (either JSON

Comment posted by mp3duck

This is what I feared. there is very little documentation on amp-html (official site, and that’s about it).

Comment posted by mp3duck

Tried that, and it didn’t work. Should be clearer. obj is not null, but it nullable properties are.

Comment posted by Ramil Aliyev

dear @mp3duck please check serialization and deserialization settings. Your updateConsentObj’s properties are camel case. Change model properties to pascal case (for example consentInstanceId should be ConsentInstanceId ) and try again.

Comment posted by mp3duck

The casing of my properties matches the payload in the network tab. I did however change and test, but as expected it had no effect.

Comment posted by Ramil Aliyev

add public accessor to updateConsentObj’s all properties @mp3duck

Comment posted by Ramil Aliyev

@mp3duck you can try HttpRequest.Form Property. All form input data included this property

By

Leave a Reply

Your email address will not be published. Required fields are marked *