I'm new to the REST API world. I explain my need: at a specific URL I have a raw JSON text, I would like this text to be acquired by my application and inserted later in the DB as a model I created previously through EF. C# NET-CORE 2.2.
if I wasn't clear enough, don't hesitate to ask me for more details.
Thanks in advance!
Edit:
I'm sorry if it' was unclear, I will provide more detail:
Actually, i have a JSON string downloaded from an url. I did it with the following code:
var client = new WebClient();
var jsonFull = client.DownloadString(string.Format("https://url"));
It's working fine. Now, I need to take from this string only a little part of the JSON, so i did:
using var jsonDoc = JsonDocument.Parse(jsonFull);
var jsonParsed = jsonDoc.RootElement;
var myCV = jsonParsed.GetProperty("cv");
CVE is an object of this JSON, and I succesfully take it.
Inside this object, there is another one called CV_data, so I extract this:
var myCVLE = myCV.GetProperty("CV_data_meta");
The result is a var with inside
ValueKind = Object : "{
"ID": "CV-2019",
"ASS": "cv#ms.org"
}"
Now, I have a class like that
public class CV_data_meta
{
[JsonPropertyName ("ID")]
public string ID { get; set; }
[JsonPropertyName("ASS")]
public string ASS { get; set; }
}
The question is: how i can put the value in the var myCVLE in the class CV_data_meta?
I tried with
var myCVClass = JsonSerializer.Deserialize<CV_data_meta>(myCVLE);
But I get an error.
Note: I can't deserialize all the string JSON into an object, because there are a lot of information that I don't need.
Thanks in advance all!
As I understand from your question, it follows:
You first need to create the JSON object mapping (class) that the API URL will return.
Then consume the API url like this:
var client = new WebClient();
var reply =
client.DownloadString(
string.Format("https://www.yourapi.com/yourpath?yourkey={0}", yourkey));
receive and map object with mapped class
var yourvar = JsonConvert.DeserializeObject<yourclass>(reply);
Now you have the API return mapped to a class in your application, you can do whatever you want with it, including saving to a database.
Related
I take from a website 24images URLs(8images and each image has 3 different sizes this is how the web API works and I cannot change it)
I get those as JSON and I parse them as shown here using newton soft JSON.
Each image URL is stored in property in the same class as its different size images
and different images are stored in other classes
so there are 8classes containing 3 properties which contains image url
I am trying to get 1image url from each class
I am trying to use reflection but since these classes has different names, it is hard to do it (for me at least)
I have came this far
PropertyInfo[] properties = typeof(Photos).GetProperties();
foreach (PropertyInfo property in properties)
{
object a = property.GetValue(mm);
//I cannot use a._1 or a._2 because it is not an instance of the class I want I also cannot convert it since the class names are not the same
}
If you are using Newtonsoft JSON library - then you can use the JObject class, it's an abstraction for any JSON object.
var uri = new Uri("Your API request URL");
using var client = new HttpClient();
var response = await client.GetAsync(uri);
var data = await response.Content.ReadAsStringAsync();
var jObject = JObject.Parse(data);
var img1 = jObject["_1"];
var img2 = jObject["_2"];
//And so on.
Please see the code below, this is how you can read properties.
Helper class to read properties
public static class Helper
{
public static void GetDataByProperty(Type photosType, object photoObject, string propertyNameToRead)
{
foreach (PropertyInfo photoProperty in photosType.GetProperties())
{
if (photoProperty.Name == propertyNameToRead)
{
foreach (var urlProperty in photoProperty.PropertyType.GetProperties())
{
Console.WriteLine(urlProperty.GetValue(photoObject));
}
}
}
}
}
Sample API data
var photos = new Photos
{
_1 = new __1() { _3 = "http://www.google3.com", _2 = "http://www.google2.com", _0 = "http://www.google0.com" },
};
Reading the data
Helper.GetDataByProperty(photos.GetType(), photos._1, "_1");
BACKGROUND
currently I am in a side project where I am building a Xamarin based mobile App for easy movie and tv show searching and navigation based on user preferences (e.g. based on movie genre etc).
To do this the mobile app acts as a client to the https://www.themoviedb.org/documentation/api online movie database's Web API service.
PROBLEM
I am looking for an easy and well supported package to wrap API Uri paths and query parameters into a web api query. I have looked a bit on RestSharp .
I like the syntax very much with the pattern based query path:
var request = new RestRequest("resource/{id}");
request.AddParameter("name", "value");
request.AddUrlSegment("id", "123");
but I am not sure about the packages future. Also if there is an alternative from MS, I would take that.
So have also looked Web-API tailored at MS alternatives but I am unsure what is recommended there. I only can find docs and examples for the server side (ASP.NET CORE MVC).
I need to be pointed to a well functioning .NET Standard 2.0 package from Microsoft or from a 3rd party to send Web API request from my Xamarin client.
Where am I
I have already written a lot of code based on pure HttPClient and AspNetcore.WebUtilituies for assembling a query string. But the gazillions of API path segments are getting out of my hand. I really need something to manage API path templates like RestSharp does
Code sample:
here I declare all of the path segments which I assemble manually ==> ugly AF
public static class WebApiPathConstants
{
public const string BASE_Address = "https://api.themoviedb.org";
public const string BASE_Path = "/3";
public const string CONFIG_Path = "/configuration";
public const string GENRE_LIST_Path = "/genre/movie/list";
...
lot of lines here
....
public const string PAGE_Key = "page";
public const string INCLUDE_Adult_Key = "include_adult";
public const string API_KEY_Key = "api_key";
public const string RECOMMENDATIONS_Path = "/recommendations";
public const string SIMILARS_Path = "/similar";
}
Here I assemble a query and kick of a task to get movie details based on the query from the server: The assembly of the Url path is my main problem. It looks too error prone.
public async Task<FetchMovieDetailsResult> FetchMovieDetails(int id, string language = null, int retryCount = 0, int delayMilliseconds = 1000)
{
string baseUrl = BASE_Address + BASE_Path + MOVIE_DETAILS_Path + "/" + id;
var query = new Dictionary<string, string>();
query.Add(API_KEY_Key, apiKeyValue);
if (!string.IsNullOrEmpty(language))
query.Add(LANGUAGE_Key, language);
string requestUri = QueryHelpers.AddQueryString(baseUrl, query);
FetchMovieDetailsResult result = await GetResponse<FetchMovieDetailsResult>(retryCount, delayMilliseconds, requestUri);
return result;
}
The result is a POCO class with the HttpStatusCode and (if successful ) a Json object. The client accesses the Json object only if the StatusCode == 2xx.
Prepared to be shot down in flames here, if this doesn't match your use-case, but it looks like the TheMovieDb site itself has a list of client libraries. It's available here: https://www.themoviedb.org/documentation/api/wrappers-libraries. They're obviously a layer higher than you're asking for here, in that they completely wrap the API, such that you need not even know what you're calling or how you're calling it, but in the interests of getting the job done, they seem like they'd do the trick.
I am getting an exception while reading the post data.
I get error on this line:
HttpContext.Current.Request.Form["UserID"].ToString();
And the error is :
System.Collections.Specialized.NameValueCollection.this[string].get
returned null.
In method I have put this code :
StreamReader reader = new StreamReader(HttpContext.Current.Request.InputStream);
string requestFromPost = reader.ReadToEnd();
and data comes in that properly like this:
{
"UserID": "1000",
"Password": "ABCD"
}
Why I am not getting value in this HttpContext.Current.Request.Form["UserID"].ToString()? I also tried Request.QueryString but no success here.
Where am I doing wrong? Any help or suggestion would be much appreciated. Thanks!
There is no Form on this request. For a request body to be interpreted as form data, it must:
have a content type of x-www-form-urlencoded
be actually formatted as form encoded values, i.e. UserID=foo&Password=bar
JSON content is JSON, it will not be interpreted as form-data.
Web API should already take care of this for you. Given an action method:
public void Action(Credentials credentials)
where the Credentials class looks something like:
public class Credentials
{
string UserID { get; set;}
string Password { get; set; }
}
You shouldn't have to do anything else to have the framework turn this incoming JSON data into an instance of Credentials and pass it to the action method. This is automatic unless you've done something strange that breaks the conventions that WebAPI expects.
I am using Sql Database rest api via c# code.
I am looking for a way to added the json to the body.
This is the format required.
{
"properties":
{
"startIpAddress": "0.0.0.3",
"endIpAddress": "0.0.0.3"
}
}
link. https://learn.microsoft.com/en-us/rest/api/sql/firewallrules/createorupdate
I'n not so used to doing this type procedure.
So, do I need to add the root 'properties' in the call? If so, how do I nest the json into the code?
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = new JavaScriptSerializer().Serialize(new
{
startIpAddress = ip,
endIpAddress = ip
});
streamWriter.Write(json);
}
I tried this, but got bad format error back.
Many thanks in advance
Scott
For the required output json you should have something link this as your anonymous object:
new
{
properties = new {
startIpAddress = ip,
endIpAddress = ip
}
}
Now it would have the properties as main object and inside it two properties that you need.
I am using RestKit from an iOS app to connect to a Web API service that we are building in C# .Net 4.
I am having the same issue from here: RestKit non-kvc object mapping
Basically C# returns something like:
formatted raw
BODY
[
{
"Id":6,
"Guid":"00000000-0000-0000-0000-000000000000",
"Owner":null,
"Message":"Testing Wom#10",
"HashTags":null,
"createdtime":"2012-10-28T00:00:00",
"PlayedCount":100,
"DurationInSecs":150.0,
"FileSizeInBytes":20000,
"FileUrl":"http://www.wom.com"
}
]
While the standard format expected by RestKit is
{"woms": [
{
"Id":6,
"Guid":"00000000-0000-0000-0000-000000000000",
"Owner":null,
"Message":"Testing Wom#10",
"HashTags":null,
"createdtime":"2012-10-28T00:00:00",
"PlayedCount":100,
"DurationInSecs":150.0,
"FileSizeInBytes":20000,
"FileUrl":"http://www.wom.com"
}
]
I don't care using a way or another, however, it seems that it would be easier from the iOS side to make C# return the "customers" class name.
How can I tell C# to return that?
Thanks.
This is the current code in my ApiController in C#:
namespace WomWeb.Controllers.Apis
{
[Authorize]
public class WomsController : ApiController
{
private WomContext db = new WomContext();
// GET api/Woms
public IEnumerable<Wom> GetWoms()
{
return db.Woms.AsEnumerable();
}
I've had some issues like this when trying to serialize JSON in C#. I think the easiest way is wrap the customer in another class. If you only need to serialize in one place you can do something like var temp = new Object { customer customer = new customer(); } right before making the call to serialize it.
This is the best solution I have found so far. Basically replace the IEnumerable by HttpResponseMessage and use the Request.CreateResponse to respond (code below).
While it works it is less than ideal: I lose the abstraction, and now the controller respond with Json regardless of the request headers (that logic was resolved automatically, but when using the CreateResponse I am writing directly to the output).
// GET api/Woms
//public IEnumerable<Wom> GetWoms()
public HttpResponseMessage GetWoms()
{
//return db.Woms.Include("Owner").AsEnumerable();
return Request.CreateResponse(HttpStatusCode.OK, new { woms = Include("Owner").AsEnumerable() });
}