Response "',' is an invalid start of a value. Path: $. in Asp.Net Core API

13,461
[RegularExpression("^[1-9]\\d*$", ErrorMessage = "Invalid fieldName.")]
public int countryId { get; set; }

the error message is set above, but actually the json you are sending can not be sent like this, you should set

"countryId": null ,
Share:
13,461

Related videos on Youtube

vivek nuna
Author by

vivek nuna

Technical Lead at NEC

Updated on June 04, 2022

Comments

  • vivek nuna
    vivek nuna almost 2 years

    I am developing API in Asp.Net Core 3.1, I have a POST method below (content type is application/JSON), I am passing an invalid JSON to the response deliberately and response is also very clear. But here my question is can I make changes to return the response like

    countryId is a required field

    for this particular case. Please let me know if it can be done, otherwise, I am fine with this response also(because this response is also valid firs to check the content type is a valid JSON or not).

    Method:

    public ActionResult ValidateFields(ValidateFieldsRequest validateFieldsRequest)
            {
    

    Request class:

    public class ValidateFieldsRequest
    {
        //string currencyCode, int countryId, string fieldName, string fieldValue
    
        [Required]
        public string currencyCode { get; set; }
        [Required]
        [RegularExpression("^[1-9]\\d*$", ErrorMessage = "Invalid fieldName.")]
        public int countryId { get; set; }
        [Required]
        [MinLength(1, ErrorMessage = "At least one field required")]
        public List<Field> fields { get; set; }
    
    }
    

    Request:

    {
        "currencyCode": "RUB",
        "countryId": ,
        "fields": [{
                "fieldName": "BIC or Bank Code",
                "fieldValue": "12345678901234567"
            },
            {
                "fieldName": "Beneficiary Account Number",
                "fieldValue": "123456"
            }
        ]
    }
    

    Response:

    {
        "type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
        "title": "One or more validation errors occurred.",
        "status": 400,
        "traceId": "|6a64d7ad-495850db788356cd.",
        "errors": {
            "$.countryId": [
                "',' is an invalid start of a value. Path: $.countryId | LineNumber: 2 | BytePositionInLine: 28."
            ]
        }
    }
    
  • vivek nuna
    vivek nuna almost 4 years
    I am talking about the case, what if I am getting request like "countryId": ,
  • MustafaOmar
    MustafaOmar almost 4 years
    actually i have never saw a request from front developer without setting value of a specified field, other wise he will remove the whole field. try removing the field from your json
  • vivek nuna
    vivek nuna almost 4 years
    Let's suppose you don't have any control on the request and client is sending you this data
  • vivek nuna
    vivek nuna almost 4 years
    Let's suppose you don't have any control on the request and client is sending you this data
  • MustafaOmar
    MustafaOmar almost 4 years
    dear you cannot leave the "countryId" empty , you have to set a value to it
  • vivek nuna
    vivek nuna almost 4 years
    but you have no control on client who is sending you the request, they can definitely send this request.
  • MustafaOmar
    MustafaOmar almost 4 years
    please read the "Unset property " part in here : ibm.com/support/knowledgecenter/en/SS8JB4/…
  • ddfra
    ddfra almost 4 years
    then the client won't get any response but that error because the json is not properly correct. The client should at least send a json with a correct syntax.
  • vivek nuna
    vivek nuna almost 4 years
    So according to this it should give countryid is required message
  • MustafaOmar
    MustafaOmar almost 4 years
    "then the property will not appear in the JSON data." according to this, you should remove the "countryId" or set it to null
  • vivek nuna
    vivek nuna almost 4 years
    Dear it's the client who's sending request and I don't have any control on the request data
  • MustafaOmar
    MustafaOmar almost 4 years
    then please contact your client, and discuss with him about it