How to get a raw json as a parameter - c#

I wanna get a raw json string from my client in my WebAPI application.
I tried it like this :
public string Get([FromBody]string rawjson)
{
}
I use Google Chrome Rest Console to try my methods first. I add my json content to RAW body and then send a get request.I put a breakpoint on my method to see if i can get the raw json data however, method invokes but rawjson comes as null. I've also tried put but that didn't work either.
What am i doing wrong ?
Thanks

GET methods cannot have Request Body and as such cannot parse values using [FromBody]. Please use the POST method.

Change the rawJson parameter type to Stream you will recieve anything that is posted on your service as as stream and than you can just read that stream to string

Related

How to prevent WCF service from wrapping CDATA in response?

I have created WCF method which return C# object as response. Now the problem is that while I am posting request to this method using chrome postman I am getting JSON response and some of parameters are wrapped with CDATA. For example, One of parameter URL has value like "http://www.examplewebsite.com/CDATA[SomeValue".
Any solution to prevent wrapping of CDATA ? Or wrapping this URL value like "CDATA[http://www.examplewebsite.com/SomeValue]" ?

Read content body from a HTTP GET in C# WebAPI

I have a WebAPI C# application. My GET method is defined as:
[HttpGet]
public HttpResponseMessage Get(string id)
This API retrieves some content from a database, based on a given id. Another parameter is required but it is so long that having it on the URL would not work, so I'm using the GET body to send such second parameter.
How can I retrieve it from inside the get method?
I tried
var dataOnBody = await Request.Content.ReadAsStringAsync();
but it doesn't work as the Get method is not async and I think it doesn't need to be that (I want a normal blocking function which reads the content of the body and outputs a string)
I just need a simple way to extract my string from the request body
Even if you somehow manage to do this, you will find that support is not universal. The HTTP specs say:
The GET method means retrieve whatever information (in the form of an
entity) is identified by the Request-URI.
So the data returned relies only on the URI, not anything in the body. Many libraries won't even let you send a request body during a GET.

Using fiddler to test my WEB Api . Which content type to select and the input parameter is always null

I created a Web API which accepts Json string and returns an XML.
I am trying to test my web API using fiddler and unable to test it.
My get method in code:
[HttpGet]
public XmlDocument GetXML([FromBody]string JsonString)
{
System.Xml.XmlDocument xmlDocument = Newtonsoft.Json.JsonConvert.DeserializeXmlNode(JsonString);
In fiddler :When I provide the content type as application json(below is screen shot). It throws a HTTP 500 error.
"An error has occurred.No MediaTypeFormatter is available to read an object of type 'String' from content with media type 'application/json'."
But when I provide the content-Type :application/xml. It successfully makes a connection to the web api but the Input parameter "JsonString" is null.
From your screenshot though it looks like you ARE in fact passing a string because it start with an equals and quotes like ="{ ... }". This looks like a JSONP body. JSON would look start with curl braces like { ... }
If you need to accept JSONP have a look at this answer:
JSONP with ASP.NET Web API

How to read Json data from Instagram RealTime API updates

I'm trying to use Instagram RealTime API to get updates whenever someone posts an image with an specific tag, I've followed all the steps, but I'm having problems to read the json data that is sent to my callback url. According to instagram's documentation :
When someone posts a new photo and it triggers an update of one of
your subscriptions, we make a POST request to the callback URL that
you defined in the subscription. The post body contains a raw text
JSON body with update objects.
I'm using C# and MVC 5, here's my code to read the "raw text JSON body", note that I'm saving the json string in a text file.
[HttpPost]
public HttpStatusCodeResult Receive()
{
string json;
using (var reader = new StreamReader(Request.InputStream))
{
json = reader.ReadToEnd();
}
File.WriteAllText(Path.Combine(Server.MapPath("~/"), "test.txt"), json);
return new HttpStatusCodeResult(200);
}
The problem is, when I receive the request, the file is create, but is always empty. There's something wrong with my code? Could it be some problem with Instagram's API? I'd appreciate some help with this.
Found it! Just set InputStream position to zero and it works!

How to send json using GET method to REST like API

I must implement method in my application which will send some data (which according to api documentation should be JSON) using GET method (it is weird...). How I can do this using c sharp in windows 8 (RestSharp lib is not working there). I don't have any experience in REST clients but I have already implement other features but there data was sended by POST or DELETE methods. I have tried "tranlate" json to get like this:
JSON:
{
a = "foo",
b = "bar
}
GET URL:
__SITE__?a=foo&b=bar
But server return null values (not error). I don't know how to deal with this thing :/
Thanks for help in advance :)
If you have api you have name of param that you should send. Just convert the data into json and sind is as this param.
If you have to send json why you`re sending param a and b as 2 diffrent strings ?
remember that a GET method can be invoked by HttpClient. Just invoke the URL
Finally it turned out (in my case) that API also accepts providing data in that way: URL?a=foo&b=bar regardless of the fact that it should be json.
Long story short, I think this will be most illuminating .. it "fills in the blanks" with using HttpClient to fire JSON Formatted data at a REST API
How do you set the Content-Type header for an HttpClient request?

Categories

Resources