How to create web service to receive JSON? - json

I am new to web services. I need to create a web service that will receive JSON from a source in VB.NET. Could you please help on how to start with this?
Earlier I have called web service and received JSON from it after getting the HTTPResponse. But in this case, source will send the data and my web service need to receive it. Thank you for your help.

After some google I was able to understand the WCF concept. All I did was create a contract with attributes as:
<OperationContract>
<WebInvoke(Method:="POST",
ResponseFormat:=WebMessageFormat.Json,
RequestFormat:=WebMessageFormat.Json,
BodyStyle:=WebMessageBodyStyle.Bare,
UriTemplate:="InputData")>
for my method.
Then I added endpoint and binding as "webHttpBinding".
Created classes to deserialize JSON and I was good to go.

Related

Does Tibco WebFOCUS only return XML Response for REST calls?

I am new to Tibco WebFocus. I want to connect to my Webfocus environment from Java code. I am going through one of the pdf document given by webfocus which is a developer guide for communication between webfocus and rest application. So in that document I see only XML response but no JSON response for a Rest call and the version of webfocus mentioned in the document is 82.07. I want to know if we can get the JSON response from Webfocus as it will be convenient and lightweight for Java.
Thanks in advance.

Configure ASP.NET MVC to handle JSON

I´m working on an ASP.NET MVC project, that does the following:
Users collect data using an android app, this data is being sent as json. This app is under development by someone else, not me, but we´re in contact.
I created a JSONController with the following action:
[HttpPost]
public JsonResult PutJSON(JSONModel json){
// do something
}
My JSONModel only contains a public string LastName for testing purposes.
For now, I can only publish the project to IIS on localhost, which can be accessed from other devices in my home network. As far as I know, that should be okay for testing, right? The project is accessible and works when entering the ip of that machine in a browser on another device in network.
Is there anything else I need to do in my ASP.NET MVC project to make it accept AJAX calls from "outside"? Am I missing something?
I tried to test with a simple AJAX call from another device, but that´s just giving me internal server errors, because of cross domain call. Accepting those cross domain calls didn´t work though (do I need this when the project is finally being published?)
I´d be very thankful if someone could help, maybe by providing a link to a tutorial explaining how to configure ASP.NET project to accept AJAX calls from the internet.
Please take a look in the following question:
How to pass JSON POST data to Web API method as object
How to receive JSON data on WebAPI backend C#?
How to receive JSON as an MVC 5 action method parameter
How to receive JSON in asp.net web API?
Hope these will help you.

Spring RESTful service returning JSON from another service

I have been creating Spring RESTful services for a while and typically I am building my own services so I create domain objects and populate them and the framework takes care of the conversion to JSON.
I have a situation now where I simply need my service to act as a pass through to another system's service that is already RESTful and returns JSON.
URL https://:/service/serviceInfo
Method GET
HTTP Content Type Produces: application/json
I simply want to wrap this call (I will apply some security checks on the service) and pass that JSON returned straight back to my service without mapping it back to Java objects, only to return as JSON to the client. Is there a simple way to do this, with minimal code?
Thanks in advance.
Can you see if this works for you?
#RequestMapping("/child")
public String testMethod(#RequestParam String param) {
return new RestTemplate().exchange("https://api.twitter.com/1/statuses/user_timeline.json", HttpMethod.GET, null, String.class).getBody();
}
You just replace the url for your own. I can also guide you to using the RestTemplate with POST or DELETE requests etc. if you need. Also adding parameters or headers if you need. I've used it extensively in some projects.

Use oAuth token with Azure MobileServiceClient.login()

I am using the native Facebook SDK (through an opensource tool called 'SimpleFacebook') to authenticate with Facebook. That part is working great. I find the Microsoft Azure implementation of Facebook authentication to be lacking.
Anyway, the next step is to use the token from this Facebook session and authenticate with MS/Azure. There are two methods like look like they should do the job
public void login(java.lang.String provider,
java.lang.String oAuthToken,
UserAuthenticationCallback callback)
Invokes Windows Azure Mobile Service authentication using a provider-specific oAuth token
Parameters:
provider - The provider used for the authentication process
oAuthToken - The oAuth token used for authentication
callback - Callback to invoke when the authentication process finishes
And another very similar method where the second param is a JSON object of type:
com.google.gson.JsonObject oAuthToken,
Is it just me or is the documentation lacking here? I tried just calling the Facebook session's .getAccessToken() and passing that to the functions and I get an error from Azure:
Caused by: com.microsoft.windowsazure.mobileservices.MobileServiceException: {"code":400,"error":"Error: invalid json"}
at com.microsoft.windowsazure.mobileservices.MobileServiceConnection$1.onNext(MobileServiceConnection.java:115)
How do we know what the correct JSON format is?
Am Using the right token?
More information can be found at:
at this Azure site
I think I have this figured out. Essentially all I had to do was create a JSON object (which is fairly new for me). I tried this earlier but I had imported the wrong JSON class (I had imported org.json.JsonObject or something rather than the com.google.gson.JsonObject).
once I did that I had to figure out what the correct json properties should be. Through a lot of Google searches I found out this is the correct format:
JsonObject jo = new JsonObject();
jo.addProperty("access_token", token);
Then use jo.toString() in the call like:
mClient.login(MobileServiceAuthenticationProvider.Facebook, jo.toString(), new UserAuthenticationCallback() {
.....
}
Really not that difficult, but why wouldn't Azure team put this in their docs???
Maybe this is just "obvious" information for a seasoned dev, but it took me a whole evening to figure out.

How to view the data from the database in json format

I am trying to pass the data from the database to the controller through a service (WCF Data Service), which is an independent application. While running the service for testing, the data is shown in XML format. I need to view the data in JSON format. Any solutions??
Thanks in Advance
For JSON support:
Download the JSONPSupportBehavior.cs from the Microsoft Code Gallery
Locate the JSONPSupportBehavior.cs file in the downloaded project and include it in your web project.
Add a reference to System.Runtime.Serialization.dll.
Mark your service with the JSONPSupportBehavior attribute.
[JSONPSupportBehavior]
public class WcfDataService : DataService<SampleDbEntities>
Please refer this link
A Beginner's Tutorial for Creating WCF Data Services
Hope this helps
WCF Data services supports JSON out of the box.
In order to receive a response in JSON format, you can include application/json in the accept header of the request. Or you can use $format=json in your request url.