controller misinterprets input? - asp.net

I have this controller
public async Task<ActionResult> SubmitFormAsync([FromBody] Dictionary<string, string> input, Guid formGuid)
for which I via postman provide the input
{
"input": {
"firstname": "Toren",
"jobtitle": "Activist",
"lastname": "SO",
"message": "",
"phone": "+123455667"
}
}
but I keep getting this error in postman
{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "00-79f401cda7f4cdb640dbd8c95dc851d5-a6b4228c3e1675ba-00",
"errors": {
"input": [
"The input field is required."
],
"$.input": [
"The JSON value could not be converted to System.Collections.Generic.Dictionary`2[System.String,System.String]. Path: $.input | LineNumber: 1 | BytePositionInLine: 12."
]
}
}
I know it means that my input is wrong, but why is it wrong I cannot figure out.

You need to post the body, without key as below --
{
"firstname": "Toren",
"jobtitle": "Activist",
"lastname": "SO",
"message": "",
"phone": "+123455667"
}
and pass the guid in query string.

Related

How to find the parent keys of a jsonpath result

I have this json obj
{
"abc": {
"some_field": {
"title": "Token",
"type": "password"
},
"some_field_2": {
"title": "Domain",
"type": "text"
},
"some_field_3": {
"title": "token2",
"type": "password"
}
}
}
And I want to get a list of keys [some_field,some_field_3] where type=password
This jsonpath $..[?(#.type=='password')] returns:
[
{
"title": "Token",
"type": "password"
},
{
"title": "token2",
"type": "password"
},
]
What should I do?
Thanks.
You are almost there, just need to add a ~ to the end to get the key(s) instead of the value(s):
$..[?(#.type=='password')]~
Note: this might not work depending on your jsonpath engine. It works on https://jsonpath.com.

Create new firestore document with REST API

I'm trying to create a new document id and store my collection inside it. My post looks like this:
{
"name": "projects/<project-id>/databases/(default)/documents/users/cena123",
"fields": {
"name": {
"stringValue": "tom"
},
"lname": {
"stringValue": "cruise"
}
ERROR i get:
error": { "code": 400, "message": "Invalid JSON payload received. Unknown name \" {\r\n \"name\": \"projects/mountain-bear-****/databases/(default)/documents/users/cena123\",\r\n
What T'm trying to achieve:
Create a document called: "cena123"
Add name and lname fields under the document
The reason you are receiving "code": 400, "message": "Invalid JSON payload received. Unknown name error, is because name should not be passed in the request body, unless it's inside the fields object.
Use the below json in your request body.
{
"fields": {
"name": {
"stringValue": "tom"
},
"lname": {
"stringValue": "cruise"
}
}
}

Firebase Firestore REST POST Request - Query and Filter using structured query

URL:
https://firestore.googleapis.com/v1beta1/projects/projectname/databases/(default)/documents/:runQuery
POST buffer:
{ "structuredQuery": {"from": [{"collectionId": "example","allDescendants": true}],"where": {"fieldFilter": {"field": {"fieldPath": "Invoiceno"},"op": EQUAL,"value": {"stringValue": "1"}}}}}
Response:
[{
"error": {
"code": 400,
"message": "Invalid JSON payload received. Unknown name \"{ \"structuredQuery\": {\"from\": [{\"collectionId\": \"example\",\"allDescendants\": true}],\"where\": {\"fieldFilter\": {\"field\": {\"fieldPath\": \"Invoiceno\"},\"op\": EQUAL,\"value\": {\"stringValue\": \"1\"}}}}}\": Cannot bind query parameter. Field '{ \"structuredQuery\": {\"from\": [{\"collectionId\": \"example\",\"allDescendants\": true}],\"where\": {\"fieldFilter\": {\"field\": {\"fieldPath\": \"Invoiceno\"},\"op\": EQUAL,\"value\": {\"stringValue\": \"1\"}}}}}' could not be found in request message.",
"status": "INVALID_ARGUMENT",
"details": [
{
"#type": "type.googleapis.com/google.rpc.BadRequest",
"fieldViolations": [
{
"description": "Invalid JSON payload received. Unknown name \"{ \"structuredQuery\": {\"from\": [{\"collectionId\": \"example\",\"allDescendants\": true}],\"where\": {\"fieldFilter\": {\"field\": {\"fieldPath\": \"Invoiceno\"},\"op\": EQUAL,\"value\": {\"stringValue\": \"1\"}}}}}\": Cannot bind query parameter. Field '{ \"structuredQuery\": {\"from\": [{\"collectionId\": \"example\",\"allDescendants\": true}],\"where\": {\"fieldFilter\": {\"field\": {\"fieldPath\": \"Invoiceno\"},\"op\": EQUAL,\"value\": {\"stringValue\": \"1\"}}}}}' could not be found in request message."
}
]
}
]
}
}
]
I have given this for POST REST function, but i am unable to get the data.
I believe the issue is in your fieldFilter, where you are using the op as EQUAL and it should be "EQUAL" with quotes, try changing it to the following and it should work:
"fieldFilter":
{
"field":
{
"fieldPath": "Invoiceno"
},
"op": "EQUAL",
"value": {"stringValue": "1"}
}
I discover you need to create an index on root Document.
So when you run the query all is fine.
https://firestore.googleapis.com/v1/projects/{projectId}/databases/(default)/documents:runQuery
"fieldFilter":
{
"field":
{
"fieldPath": "Invoiceno"
},
"op": "EQUAL",
"value": {"stringValue": "1"}
}{ "structuredQuery": {"from": [{"collectionId": "example","allDescendants": true}],"where": {"fieldFilter": {"field": {"fieldPath": "Invoiceno"},"op": EQUAL,"value": {"stringValue": "1"}}}}}

Responding to slack view_submission event with error object

I use ASP.NET Core 2.1 to build integration with Slack
Here is my modal in json format.
{
"type": "modal",
"callback_id": "send_sms",
"title": {
"type": "plain_text",
"text": "Send SMS",
"emoji": false
},
"submit": {
"type": "plain_text",
"text": "Submit"
},
"close": {
"type": "plain_text",
"text": "Cancel"
},
"blocks": [
{
"type": "divider"
},
{
"type": "input",
"block_id": "phone_number",
"label": {
"type": "plain_text",
"text": "Enter phone number",
"emoji": false
},
"element": {
"type": "plain_text_input",
"placeholder": {
"type": "plain_text",
"text": "Phone number",
"emoji": false
},
"action_id": "action_phone_number"
}
},
{
"type": "input",
"block_id": "message",
"label": {
"type": "plain_text",
"text": "Enter message",
"emoji": false
},
"element": {
"placeholder": {
"type": "plain_text",
"text": "Message",
"emoji": false
},
"action_id": "message",
"type": "plain_text_input",
"multiline": true
}
}
]
}
So when user submit form, my .net core application recieves an view_submission event and if phone number has invalid format I want to response to this event with error.
Slack docs says that I should response with such json object:
`{
"response_action": "errors",
"errors": {
"phone_number": "Invalid phone number format"
}
}`
During debugging I found out that my app actually DO load json from file and DO respond with a string that contains this json. But a still get this error on my modal
enter image description here
controller method returns Task> object but I'm not sure that it's correct
So my question is:
Does anybody know how I should response to this slack event using .net core?
As far as I understand even if I have validation error in my .net core app and I want to return error object to slack, I should respond with status 200 OK
Finnaly this question is resolved by returning
Content('json_string', "application/json");

Swagger Schema error should NOT have additional properties

I am trying to create swagger json and trying to check it's validity in
http://editor.swagger.io
Upon validating the json, the above mentioned editor gives the following error:
Schema error should NOT have additional properties
additionalProperty: components
Jump to line 0
If for some reason I can't define an element named components at root level where i am going to have some sort of json schema, what is the right way to do a $ref on the schema for requestBody for an API operation as I intend to do as seen in my example below. Also, do you see anything wrong with my swagger json ?
My swagger json for swagger2.0 look like this.
{
"swagger": "2.0",
"info": {
"version": "1.0",
"title": "My swagger API",
"contact": {
"name": "myName",
"email": "abc#gmail.com"
}
},
"host": "localhost:1234",
"basePath": "/",
"tags": [{
"name": "someTagName",
"description": "this is a try"
}],
"components":{"schemas": {"myEndPoint": ""}},
"paths": {
"/myEndPoint": {
"post": {
"tags": ["some-tag"],
"summary": "myEndPoint endpoint will give you something",
"description": "some description will go here",
"operationId": "getMyEndPoint",
"consumes": ["application/json"],
"produces": ["application/json"],
"parameters": [{
"in": "body",
"name": "somePayload",
"description": "somePayload is what this is",
"required": true,
"schema": {
"$ref": "#components/schemas/myEndPoint"
}
},
{
"in": "header",
"name": "Authorization",
"description": "auth token goes here",
"required": true,
"type": "string"
}],
"responses": {
"200": {
"description": "Success",
"schema": {
"type": "string"
}
},
"400": {
"description": "Bad Request"
}
}
}
}
}
}
You are mixing up OpenAPI 3.0 and 2.0 syntax. The components keyword is used in OpenAPI 3.0. In OpenAPI/Swagger 2.0, reusable schemas live under definitions:
"definitions": {
"myEndPoint": {
...
}
}
Make sure to also change the $ref to
"$ref": "#/definitions/myEndPoint"

Resources