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 ,
Related videos on Youtube
Comments
-
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 likecountryId 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 almost 4 yearsI am talking about the case, what if I am getting request like
"countryId": ,
-
MustafaOmar almost 4 yearsactually 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 almost 4 yearsLet's suppose you don't have any control on the request and client is sending you this data
-
vivek nuna almost 4 yearsLet's suppose you don't have any control on the request and client is sending you this data
-
MustafaOmar almost 4 yearsdear you cannot leave the "countryId" empty , you have to set a value to it
-
vivek nuna almost 4 yearsbut you have no control on client who is sending you the request, they can definitely send this request.
-
MustafaOmar almost 4 yearsplease read the "Unset property " part in here : ibm.com/support/knowledgecenter/en/SS8JB4/…
-
ddfra almost 4 yearsthen 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 almost 4 yearsSo according to this it should give countryid is required message
-
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 almost 4 yearsDear it's the client who's sending request and I don't have any control on the request data
-
MustafaOmar almost 4 yearsthen please contact your client, and discuss with him about it