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?
Related
I am currently creating a simple game that can be played from the Postman console, as a .NET Core Web API project.
I can POST data to the game from Postman without difficulty but I'm having some problems setting up a call to an external webservice at the point at which the POST is made.
What I want to do is receive a POST, and then make an external API call for some random numbers from an external service that provides random numbers.
This method would seem to need to be inside the POST method to ensure that it's triggered after each POST request and generates new numbers each time.
I have created a ProcessRandomNumbers asynchronous method using HTTPWebClient that returns a value of 'msg' (message) and then I am trying to call that method in my POST, then pars the string to numbers and assign the values and use them for calculations. However, I can't seem to access the data ('msg') returned from the method inside of my POST request?
The code inside my POST method is below:
await ProcessRandomNumbers();
if (diceRoll.Roll == true)
{
diceRoll.DiceRoll = msg;
}
Thanks for any help anyone can provide!
Looks like ProcessNumbers() only trying to get the numbers from external website, so this method can be renamed as GetRandomNumbers().
Here is the code snippet where I returned the API response back to caller:
private async Task<string> GetRandomNumber()
{
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Add("User-Agent",
".NET Foundation Repository Reporter");
return await client.GetStringAsync("https://www.random.org/integers/?num=2&min=1&max=6&col=2&base=10&format=plain");
}
In the caller assign the return value to msg field, and use it.
I am new to asp .net and doing some homeproject. Hope you can help!
I have a ASP.NET Web API application with texbox that takes order number as serch string.
I find my object with this code:
var query = ReadFiles()
.Where(n => n.orderNumber == TextBox1.Text)
.Select(n => n);
After that Im trying to convert it to JSON witht his code:
var json = new JavaScriptSerializer().Serialize(query);
TextBox1.Text = json;
I get a String back that says: JSON Visualizer
Now for the question. How do I get this JSON string to the UI, I would like it to show up in some sort of table? I am new to asp .net with that in mind I hope this question is not to stupid. Oh and I use C#
Kind regards.
If you are using a Web API controller class, you can create a method endpoint that returns the data, and then use an Ajax request on the client to that end point.
This is a nice example: Getting Started with ASP.NET Web API 2. You should also look at Routing in ASP.NET Web API to understand the routing mechanism.
In your particular case, you could set up an endpoint to post the order number to from the client's text box (using Ajax). The controller method will then receive the order number as a parameter, and then fetch the data it needs on the server. You would then return the JSON response from that method. The client's success callback function from the post call will receive the JSON response, and then it's up to you to bind that to the markup with JavaScript.
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.
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
I am currently experimenting with the HTTP request. I have successfully managed to do get requests and I have read on doing post request with HTTP request.
Now I am trying to work with the yahoo API and in order to use the Yahoo api it states that at
The Message Management API can be used to send a message to another
Yahoo! Messenger contact. The API is very simple to use, as shown
here. Note that the contact that the message is sent to is part of the
URI, using the following format:<server>/v1/message/<network>/<contactID>
POST /v1/message/yahoo/targetYahooId?sid=msgrsessionid
Host: rcore1.messenger.yahooapis.com
Authorization: < Standard OAuth credentials >
Content-Type: application/json;charset=utf-8
Content-Length: 25
{
"message" : "Hey there"
}
Now I have an OAuth string which I obtained from get using the HttpWebRequest object.
The string is something like this
oauth_token=A%3Dvh....aRg--&oauth_token_secret=bd46a....c9239&oauth_expires_in=3600&oauth_session_handle=ALtT.....3J1N4Zg--&oauth_authorization_expires_in=784964948&xoauth_yahoo_guid=TUSKED5...NCIA
UPDATE
Now my question are as follows :
1- If I am using WebRequest object in C# what would my URI look like
2- I understand that it requires a JSON type object. How do i even know what OAuth parameters are ?
One thing you'll need to change is the content type:
request.ContentType = "application/json;charset=utf-8";
And of course, the url.
you need to change the url on the line with the url in it
you need to change the content-type line
you need to make the payload into a json string then convert it to a byte array (byteArray in the sample)
either assemble the json by hand "{ foo:'bar'}" etc or use json.net
and set the content-length
Looks like it's expecting a JSON object for the request body. Depending on the version of .NET you're using, you can either use a Javascript serializer as shown here (https://stackoverflow.com/a/7003815/939080) or JSON.NET (http://james.newtonking.com/projects/json-net.aspx) to convert your form collection into JSON output.
You are asking an open-ended question that would require people to write a bunch of code for you if you want a specific and complete answer. As others have pointed out, there are several issues that you'd need to deal with:
The JSON payload, which would be a straightforward matter of putting the JSON string in the request body via the byteArray used in the code sample.
The content type, which you would need to change as described by jrummell.
The OAuth credentials, which is a kettle of fish you'll need to read about, understand, and acquire a library for. Here's a good place to start looking for a library.