Web api deserialization - c#

I receive in a request from a web api project a parameter that represents the URL to a json or xml file.
What i have to do is to download this file, deserialize it and the process it.
In the past i was writting two individual methods :
one method that was reading the file using linq to xml
another method that reading the file using newtonsoft.json
I am wondering is there a way so that web api can deserialize automatically this url link / file for me ?

Related

How to load XML files using Dropbox.api V2

I migrated my project from Dropbox API version 1 to 2 and it returns error code 400 (Bad request) when I try to load xml file from Dropbox.
I have read the documentation but somehow I still don't understand how to correctly structure version 2 URLs.
What would be the correct V2 url to load an XML file?
I'm loading XML like this:
XmlDocument xmlDoc = new XmlDocument();
string uri = new Uri(string.Format(...)).AbsoluteUri;
xmlDoc.Load(uri);
Version 1 (deprecated)
string.Format("https://content.dropboxapi.com/1/files/auto{0}?access_token={1}", svcUri, ACCESS_TOKEN)
Version 2 (current)
// What is wrong here??
string.Format("https://content.dropboxapi.com/2/files/download{0}?access_token={1}", svcUri, ACCESS_TOKEN)
In your version 2 code, you're attempting to put the file path directly on the URL path, and are passing the access token in an access_token path. These worked on API v1, but API v2 is a different interface so those won't work there.
In API v2, you're correct that the replacement is /2/files/download. That's a "content-download endpoint", so the standard way of using that is via a POST with 'Authorization' and 'Dropbox-API-Arg' headers.
To just use a GET with it instead though, as it appears you want to do, i.e., so you can just use a URL by itself, you can use the URL parameters documented here under "Request and response formats".
So, to access a file at "/folder/filename.xml" the API call parameters for /2/files/download would be:
{"path": "/folder/filename.xml"}
URL encoding those for use with the arg URL parameter, along with the access token information in the authorization URL parameter, the result would be:
https://content.dropboxapi.com/2/files/download?authorization=Bearer%20ACCESS_TOKEN&arg=%7B%22path%22%3A%20%22%2Ffolder%2Ffilename.xml%22%7D
Be sure to replace ACCESS_TOKEN with the actual access token.

.NET Web API WebHook won't accept XML

Via Postman I am receiving the the following response:
The WebHook request must contain an entity body formatted as JSON
With the help of some online tutorials I set up a webhook receiver that successfully reads in events from GitHub.
The next hurdle is being able to to receive a request containing XML data. I brought in the relevant WebHooks.Custom packages via NuGet so that I can handle a less specific request. I'm able to get into the ExecuteAsync method in my CustomWebHookHandler class and read in the data if it's sent as JSON, but if I change the raw body of the request to XML (along with the content-type in the header) I get the error listed above.
Do .NET Web API projects not handle XML out of the box? All commments I found elsewhere stated that they do handle XML.
If they don't handle XML out of the box how do I change the app to allow XML?
If they do handle XML out of the box why am I getting the error message above?

How to update a JSON file from Unity and send it back to the server

I am a game developer, but I am new to web development with JSON. However, I am trying to learn how to implements an online high score system by using a JSON file stored on my web host server with a Unity project. So far, I can get user names and their scores from the remote JSON file by using the WWW class in Unity. However, I am not sure what would be the best way to update existing /add new pairs of data to the JSON file. I have been seeking for proper documents/tutorials online for hours, but I didn't find one that works for me. Can anyone please provides some hints and suggestions for doing this from both client side(Unity) and server side? Thanks in advance!
Check on the link below the library for JSON manipulation
http://wiki.unity3d.com/index.php/SimpleJSON
SimpleJSON is an easy to use JSON parser and builder.
http://wiki.unity3d.com/index.php?title=JSONObject
JSONObject :
Decode JSON-formatted strings into a usable data structure,
Encode structured data into a JSON-formatted string,
Interoperable with Dictionary and WWWForm
Both libraries allows you to edit existing values and add new ones to your original JSON
To send your JSON back to the server you can use WWWForm : http://docs.unity3d.com/ScriptReference/WWWForm.html

Posting file from mvc controller to webAPI service

a little background on our system, we have a mvc application that creates and displays forms, then posts these forms to a controller within the mvc application. it then does verification etc. etc.
I want to be able to use this to upload a file (currently using a post with the contoller pulling out the httppostedfilebase) have it send that file to a seperate application API which will pull the file information, store the information in the database, and save the file as something generic.
I have a method that can do all the pull apart/save file stuff, I have a controller that accepts my form post and gets all the relevant data including an httppostedfilebase. What I need is a way to send that file (which is not saved yet) over to our API.
We are hoping to avoid turning the file into a base64 string.
This is in c#.
Have you looked at this answer:
Web API: how to access multipart form values when using MultipartMemoryStreamProvider?
I think it will provide some ideas on how to handle streaming files in memory.
Solution we used:
using HttpPostedFileBase from the multipart form,
create byte array
stream file contents into the byte array
convert byte array to base64 string
add to json object along with file headers (name and extension)
post json object to api using HttpClient

.net Client consuming Axis2 Web Service

I have an .net 2.0 C# client app that has a web service reference to an Axis2 Java Webservice.
The idea is to send some xml data to the webservice, so it can be parsed and inserted into database.
The WS method accepts a single parameter of type 'xsd:anytype'.
Java web service:
public class JWS{
public Response AddData(Object inputXML) {
return Response;
}
}
C# Client:
JWS client = new JWS();
object inputXML = "<xml>some xml data</xml>";
response = client.AddData(inputXML);
There are 2 issues i am seeing when monitored using fiddler.
1) The request has an additional element '<inputXML>' added before the actual xml data.
<inputXML><xml>some xml data</xml></inputXML>
2) The xml is encoded, so '<' is appearing as "<"
I am not sure if this is how SOAP request's are generated but i would like to remove the <inputXML> tag and also, have the xml appear as is without having to replace the special characters.
Is this possible? Is it got something to do with 'Wrapping'/'UnWrapping' Types?
Also, i have used SoapUI to test the java web service and it works well. However, in the request tab, i had to manually remove the <inputXML> tag and submit for it to work correctly. Please help.
TIA
This is expected behaviour under SOAP and the inputXml variable will be decoded back to the original string when passed to your web service method.
However this may indicate a problem with your design, have you considered constructing an object to send to your web service rather than xml data? (As this object will transparently be converted to xml for the web service call anyway).
I found out that the issue is not with encoding but it was interpreted incorrectly on java side when the message was viewed in axis2. So, it is getting decoded properly. Also, the inputxml is now being handled correctly.

Categories