C# Firestore API POST - Cannot find field - c#

I have hit a bit of a brick wall here. I am working in client side c# code and using the firestore API. I cannot post a document (with data) in. I am able to create a document by removing the line ".AddJsonBody(testdata)" below.
CODE
string url = "https://firestore.googleapis.com/v1/projects/<MYPROJECT>/databases/(default)/documents/Users";
var client = new RestClient(url);
string testdata = "{\"foo\":\"bar\"}";
var request = new RestRequest()
.AddJsonBody(testdata);
var response = await client.ExecutePostAsync(request);
ERROR
{
"error": {
"code": 400,
"message": "Invalid JSON payload received. Unknown name "foo" at 'document': Cannot find field.",
"status": "INVALID_ARGUMENT",
"details": [
{
"#type": "type.googleapis.com/google.rpc.BadRequest",
"fieldViolations": [
{
"field": "document",
"description": "Invalid JSON payload received. Unknown name "foo" at 'document': Cannot find field."
}
]
}
]
}
}
I have tried using document:commit. I have tried various different methods of parsing the data, all keep getting similar errors.

Related

Sending email with Azure logic app after triggering via httpRequest

I've a azure logic app which contains two operations.
When a httpRequest is received.
Send email (v2)
I'm trying to send an email with the content of "task" when triggered by the httpRequest.
HttpRequest body json schema:
"properties": {
"due": {
"type": "string"
},
"email": {
"type": "string"
},
"task": {
"type": "string"
}
},
"type": "object"
}
I've added the "task" object from the dynamic content in the body of the email. I want the body of the email to contain the content of the "task" sent in the httpRequest.
Below is how I'm sending the httpRequest with the required json data. However, the email is always empty with no body. I must be doing something wrong. Any pointers would be appreciated.
await client.PostAsync(
logicAppUrl, new StringContent(jsonData, Encoding.UTF8, "application/json"));
jsonData content:
{"email":"test#gmail.com","due":"4/1/2020","task":"content1-------------"}
You can debug with Postman what your json should look like. It seems that you 1) either forgot to include the curly braces in your string or 2) forgot to escape your double quotes in the string. It should look like
string jsondata = "{ \"email\": \"test#gmail.com\", \"due\": \"4/1/2020\", \"task\": \"content1-------------\"}"
EDIT:
If the json doesn't match, go to the logic app's HTTP task, click Use sample payload to generate schema and copy your json into it. In your case, it should be
{
"type": "object",
"properties": {
"email": {
"type": "string"
},
"due": {
"type": "string"
},
"task": {
"type": "string"
}
}
}

Create Item in Dynamics 365 Business Central using Rest API

I am trying to consume Microsoft Dynamics Business Central Rest API, to create item using following endpoint:
https://api.businesscentral.dynamics.com/v1.0/mydomain.com/api/v1.0/companies({id})/items
Following is my code:
string requestBody = JsonConvert.SerializeObject(itemBodyValues);
string url = "https://api.businesscentral.dynamics.com/v1.0/mydomain.com/api/v1.0/companies({id})/items";
string encodedCredentials = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(userName + ":" + WebServiceAccessKey));
HttpWebRequest endpointRequest = (HttpWebRequest)HttpWebRequest.Create(url);
endpointRequest.ContentType = "application/json";
endpointRequest.Method = "POST";
//endpointRequest.Accept = "application/json;odata=verbose";
using (var streamWriter = new StreamWriter(endpointRequest.GetRequestStream()))
{
streamWriter.Write(requestBody);
}
endpointRequest.Headers.Add("Authorization", "Basic " + encodedCredentials);
HttpWebResponse endpointResponse = (HttpWebResponse)endpointRequest.GetResponse();
Following is the request body (copied it from here):
{
"number": "1896-S",
"displayName": "ATHENS Desk",
"type": "Inventory",
"blocked": false,
"baseUnitOfMeasure": {
"unitCode": "PCS", //Unit of measure with this code exists in dynamics BC
"unitName": "Piece",
"symbol": "",
"unitConversion": null
},
"gtin": "",
"itemCategory": {
"categoryId": "TABLE", //Item category with this code exists in dynamics BC
"description": "Assorted Tables"
},
"inventory": 0,
"unitPrice": 1000.8,
"priceIncludesTax": false,
"unitCost": 780.7,
"taxGroupCode": "FURNITURE"
}
When i try to execute the code, then at endpointRequest.GetResponse(); i get the following error:
The remote server returned an error: (400) Bad Request.'
I tried creating item in postman (basic authentication), with same URL and request body and error is:
{
"error": {
"code": "BadRequest",
"message": "Does not support untyped value in non-open type. CorrelationId: 4bc23d7b-f6b3-4eca-ab62-6fb7d37e23ac."
}
}
It is important to note that item successfully gets created when i exclude baseUnitOfMeasure and itemCategory properties from request body. But including these properties causes the error. As i researched for the issue above, from different sources i found that, such issue appears when a field/property is mistyped. I am copying the request body from Microsoft document as mentioned above so i am clueless that which field is causing the issue. Please help me resolve this. Thanks
For Future visitors/readers:
It took some R&D to let me know that the request body that can be found in Microsoft Document actually contains some wrong property name.
we can go to https://api.businesscentral.dynamics.com/v1.0/[your domain]/api/v1.0/$metadata where after authentication we get the XML that specifies the property names that should be used. So, the request body that i found working is:
{
"number": "1836-S",
"displayName": "ATHENS Desk",
"type": "Inventory",
"blocked": false,
"baseUnitOfMeasure": {
"code": "PCS",
"displayName": "Piece",
"symbol": "",
"unitConversion": null
},
"gtin": "",
"itemCategory": {
"code": "TABLE", //make sure item category with this code doesn't already exists
"displayName": "Assorted Tables"
},
"inventory": 0,
"unitPrice": 1000.8,
"priceIncludesTax": false,
"unitCost": 780.7,
"taxGroupCode": ""
}
BadRequest is usually a bad datatype or json that is in the request body is not what is expected by the server side. I don't recommend adding any fields to the request body that are null this can also cause bad request.
My question is where is the field weight or net_weight on the item card to update via the rest api? Do you have to build a custom api just to include a field in the table not visible in the api to update?

Parsing JSON Using Newtonsoft.Json Without Knowing the Structure

I'm working on a project that involves automating API calls using a Swagger Definition. I download the swagger.json file. The structure of the JSON Object I need to parse is not consistent. When parsing paths, there are a list of objects, then within that they have the methods that can be used for that specific path. I can retrieve just the path using various string methods but my question was, is there a good way to parse json if the JSON is structured in such a way that it does not have a firm key? Here is an example of what I mean:
{"/user": {
"post": {
"tags": [
"user"
],
"summary": "Create user",
"description": "This can only be done by the logged in user.",
"operationId": "createUser",
"consumes": [
"application/json"
],
"produces": [
"application/json",
"application/xml"
],
"parameters": [
{
"in": "body",
"name": "body",
"description": "Created user object",
"required": true,
"schema": {
"$ref": "#/definitions/User"
}
}
],
"responses": {
"default": {
"description": "successful operation"
}
}
}
}
If I wanted to just parse that path and retrieve the method object how could I go about that considering sometimes the object will be "post" or sometimes it will be "get", "put", etc depending on what is allowable for the path.
JObject jsonResp = swaggerDownload();
JObject paths = (JObject)jsonResp["paths"];
foreach (var i in paths)
{
string pathToString = i.ToString();
var shaveSomethings = pathToString.Substring(1, something.Length - 2);
var pathAndJson = shaveSomethings.Split(new[] { ',' }, 2);
string correctJsonStructure = "{\"" + pathAndJson[0] + "\":" + pathAndJson[1] + "}";
JObject bd = JObject.Parse(correctJsonStructure);
//dynamic pathsTest = JsonConvert.DeserializeObject<dynamic>(correctJsonStructure);
//JObject result = JsonConvert.DeserializeObject<JObject>(correctJsonStructure);
//Console.WriteLine(bd["/user"]);
}
The swagger.json file should have full definition of each entity that endpoints return. You can follow How to create Rest API client to get a working client.
I've dealt with an API where responses didn't always match the definition. I saved all responses to a store/log first and then would try to de-serialize JSON. In case of an exception I would go back to store/log and see what was different and update my code to accommodate for the change. After few iterations there were no new changes and the ordeal was over.
Hope that helps.

Asp.net Mvc receives a complex object as a list of key/value pair array

I have two asp.net mvc apps that am trying to get to communicate with each other by sending a complex serialized object from one of the app to the other.
But when the data reaches the other end, the object comes in as a list of array of key-value pair. Below are the details of the what am trying to send/recieve.
Here is the code that am using to send the json object
using (var hc = new HttpClient())
{
hc.DefaultRequestHeaders.Accept.Clear();
hc.DefaultRequestHeaders.Accept.Add(Media);
var dict = new Dictionary<string, DejaVuObject> {{"Entity", obj}};
var strinified = JsonConvert.SerializeObject(dict);
var stringContent = new StringContent(strinified, Encoding.UTF8, "application/json");
var response = await hc.PostAsync(url, stringContent);
return response;
}
The receiving method signature
[HttpPost]
public ActionResult RecieveEntity(Dictionary<string, object> post)
Here is what am sending
{
"Main Service ID": "1",
"Node ID": "",
"Parameters": [
{
"Name": "firstname",
"Validation": {
"Entity": 0,
"EntityListFilter": "",
"IsNotEditable": false,
"IsPrimaryIdentifier": false,
"IsRequired": true,
"IsUnique": false,
"Parameter Format": 0,
"ParameterMode": ""
}
}
],
"CustomEvents": [
{
"Description": "event description",
"Message": "new message",
"MilestoneCondition": "milestone information.",
"Name": "new message",
"TheFields": []
}
],
"Processings String": "Action failed.[TN01-31:Action failed]"
}
Here is what am receiving
{
"Processings String": "Action failed.[TC01-71:Action failed while processing the request.]::Action succeeded.[TC01-54:Processing this command was successful.]",
"Parameters": "[Name, Firstname][Validation, ][Key, 2e431711-2ba9-40ef-985e-dbfa8c13a932][isrequired, True][fieldname, ][Name, Lastname][Validation, ][Key, be4de2d6-d39e-44fa-8f31-b4b0964f82da]",
"CustomEvents": "[Description, Processing this command was successful][Message, Action suceeded][MilestoneCondition, When command processing suceeds.][Name, Action suceeded][TheFields, System.Collections.Generic.List`1[System.Dynamic.DejaVuObject]]",
"Main Service ID": "1"
}
If you look properly in what comes into the other system, you will find that the of most of the inner object is an array of key/value pair instead of ordinary array of objects that was sent. What am I doing wrong, and how do i go about correcting it?
Its the fact that you're using a dictionary type to receive the content. At the point the data packet is sent from the app1 its as per your definition. So updating app1 isn't going to help you.
As #Krishna has suggested update the signature to use dynamic, or you can extend your current app2 method to deal with the new interface.
Do you have an example of the data packet from one of the existing integrated apps?

Valid JSON string throws Unexpected character encountered error

I'm trying to connect an angular2 client to a C# ASP.net core server. When I'm sending a JSON string using websockets from the client to the server, I get an error:
Unexpected character encountered while parsing value: {. Path 'Argument', line 1, position 39.
The JSON string is as follows (The error seems to originate from the opening bracket after "Argument:"):
{
"MethodName": "CreateUser",
"Argument": {
"User": {
"Attributes": [{
"Name": "age",
"Value": "30",
"Type": 0
}],
"Email": "test#mail.com",
"Name": "Test Name"
},
"Password": "1234"
}
}
The code that throws the error is here:
public string Receive(string input)
{
try
{
Debug.WriteLine(input);
InstructionServer jsonObject = JsonConvert.DeserializeObject<InstructionServer>(input); // This fails
string methodName = jsonObject.MethodName;
string data = jsonObject.Argument;
return methods[methodName](1, data, "", "");
}
catch (Exception e)
{
return "error: " + e.Message;
}
}
I can't seem to figure out what the error is, because when I throw the JSON into an online JSON Formatter, it reports it as valid JSON. Any help would be appreciated.
EDIT: Just to clarify on the valid JSON. I tried printing the json string out before sending it on the client and after receiving it on the server, and it is the same json string as written above.
Argument appears to be expecting a string, but found an object. You'll have to check the format that InstructionServer is expecting and ensure that it will be able to deserialize correctly.
The Receive method expect the string value, this means you have to convert your object to JSON format to be like this:
"{\"MethodName\":\"CreateUser\",\"Argument\":{\"User\":{\"Attributes\":[{\"Name\":\"age\",\"Value\":\"30\",\"Type\":0}],\"Email\":\"test#mail.com\",\"Name\":\"Test Name\"},\"Password\":\"1234\"}}"

Categories

Resources