Multiple parameters send as json in body RetroFit without creating a model to hold it - retrofit

I want to use RetroFit for Android but I donĀ“t like the idea of creating a model for every single request.
Can I create a Service that receive like 2 strings and convert it into a json that should go in the body of the post request?

You can create a HashMap and put it as parameter of your request method. For example, something like this:
#Body HashMap user

Related

How to add query string parameters to GetFromJsonAsync in a Blazor app?

I'd like to add some query string parameters to the GetFromJsonAsync helper method from the 'System.Net.Http.Json.HttpClientJsonExtensions' library. Reading through the docs and examples, it seems like this helper is more for vanilla API calls that do not provide a lot of intervening for custom headers or parameters, but I don't have clarity on this. It appears to add custom parameters or headers, the preferred method is to use the more raw, HttpClient.GetAsync Method.
I suppose I can just string manipulate the requestUri parameter of GetFromJsonAsync but I'm not seeing this as a mainstream practice. I just want to add some simple query string parameters like the following:
'zip': 90210
'units': 'imperial'
What is the correct or mainstream way to manipulate the Http call to an API for parameters or headers in a Blazor application?
I found this works
var data = await Http.GetFromJsonAsync<MenuApiModel>($"{nameof(Menu)}?menuId=1");
Notice I simply put the query param in the url itself.
I hope that helps.

Can I configure asp.net so some responses are serialized as camelCase and some as PascalCase?

I have an API with two clients: OldClient and newClient. I currently have this in Startup.cs so my json responses are serialized as PascalCase, i.e. as per all my .net objects which have first letter capitalized.
services.AddControllers().AddJsonOptions(jsonOptions =>
{
// So json output is like 'SomeId' instead of 'someId':
jsonOptions.JsonSerializerOptions.PropertyNamingPolicy = null;
...
}
OldClient loves this format. However, newClient would really prefer camelCase.
Is there a way I can configure my app to respond with camelCase for newClient requests and PascalCase for OldClient requests? newClient can send a header to indicate that it wants camelCase.
You can check out this issue on aspnetcore's github page
The possibility of using specific JSON Serializer Options on a per-controller basis hasn't been implemented yet. It has been moved to ".NET 8 planning", which means it's still a ways-away.
Meanwhile, you could work around this issue by:
For data reception and model-binding, you could create a Custom ModelBinder by implementing IModelBinder interface in a ModelBinderAttribute in order to utilize your specific JSON Serialization options. Then, you could simply add the attribute to the endpoints where you need it.
For data responses, you could simply use:
return new JsonResult([object], [CustomJSONSerializationSettings]);
It's quite annoying to have to modify these per-endpoint, but it seems like it's the only way until the feature is added in .net 8 (if we're lucky).

Firebase REST API get all data from node as JSON Array

Is there any possibility in Firebase to get data from node as JSON array, but not like multiple objects
For example I have:
https://additive-food-e2a9a.firebaseio.com/additive.json
{"-KjrTAOehl0RAvKYlYVp":{"alias":"Curcumin","danger":"2","name":"E100"},"-KjrUIm-itn8h8EjckNQ":{"alias":"Curcumin","danger":"2","name":"E100"},"-KjrXlvuJjMuogCJmGDK":{"alias":"Curcumin","danger":"2","name":"E100"}}
But I need something like:
[{"-KjrTAOehl0RAvKYlYVp":{"alias":"Curcumin","danger":"2","name":"E100"},"-KjrUIm-itn8h8EjckNQ":{"alias":"Curcumin","danger":"2","name":"E100"},"-KjrXlvuJjMuogCJmGDK":{"alias":"Curcumin","danger":"2","name":"E100"}]
Firebase returns only JSON object when you hit GET request to the node. You have to apply JSON parsing logic in Java, Javascript or whatever language you use.
Sample JSON Parsing logic in Java.

JSON + SOAP - Is DataContract necessary?

Here's my problem.
I'm using SOAP to retrieve information from a third-party web service.
Response time is too high, so I was planning on using JSON instead, at least in a couple of methods.
For this I'm using DataContractJsonSerializer, but I seem to be having some trouble.
For example, in SOAP there's a method called getAvailablePublic with returns an object of type getAvailablePublicResponse.
There's an equivalent for this method in JSON, which also returns a an object of type getAvailablePublicResponse.
In order to deserialize the information I needed to create a couple of data contracts, and here are my concerns:
Do I really need to create a DataContract? Why can't I use getAvailablePublicResponse object from asmx?
The problem is that if I create a DataContract, I need to use a different name other than getAvailablePublicResponse, as I would have two objects with the same name (the one created by me, and the one from SOAP), and this would require making several changes in my solution.
Hope this makes sense.
Thanks.
Can you post your client code that is making the call to the web service? I don't know what you are using now, but I'm a fan of RestSharp for making remote calls and serializing JSON to C# classes. Something like this:
RestClient client = new RestClient("http://some.domain.com/someservice?someparam=yes");
var results = client.Execute<MyGreatDTOClass>(new RestRequest(Method.GET));

How to correlate the request and response for the purpose of logging

I'm using the Global filters to log the request/response which works fine, but now I'd like to embed a correlation ID (Guid) in both to that I may tie the 2 together. What's preferred way to do this?
Thank you,
Stephen
Not sure if you are taking about these but you could do this with ServiceStack Request and Response filters Sounds like you want to do something like this...
Create the Guid in the Request Attribute Filter and save to db.
While still in the request filter store that Guid in into the IHttpRequest.Items dictionary. This dictionary is for you to use for anything you want. It will be avilable to all the ServiceStack services.
Access that Guid in the Response Attribute filter via the IHttpRequest variable and save response entry to database.

Resources