c# posting to API via button click - c#

I've created a form that will Generate JSON code when filled in, i need it to post to an API on a button press, using RestSharp.
I've tried adding the richTextBox1.Text into the JsonBody and in the RestRequest but still it doesn't get a response.

Related

Using RestSharp to submit a POST request, values are added to header

I am trying to submit some data to an API using RestSharp in C# and it seems all of my parameters are added to the Headers collection - or that is just how they are catalogued in VS.
Here is my code
var client = new RestClient("https://api.com");
var request = new RestRequest("/recognize", Method.POST);
request.AddHeader("app_id", "");
request.AddHeader("app_key", "");
request.AddHeader("Content-Type", "multipart/form-data");
request.AddParameter("gallery_name", model.gallery_name);
request.AddParameter("image", model.image);
var response = client.Execute(request);
The error back from the API says the request is missing the gallery_name and image parameters, but looking into the request object they are there in the Headers collection.
I can make the call in Postman where the method is set to Post and the Body is set to form-data, along with 2 key/value pairs listed.
What am I doing wrong?
In your question you says "I can make the call in Postman ...".
Normally if the Postman request is successful then should the generated RestSharp code from Postman works, if there are no open issues in the RestSharp library that can cause your problem.
It's hard to debug and reproduce the problem from here, but I can give the following that you can check.
Check this:
If your Postman request works and is successful then check the Restsharp code generated by Postman:
Click on "code"
Choose in the combobox for "C# (RestSharp)"
Try the RestSharp code that you see in the window, if the code doesn't work check this URL for known issues.
On a sidenote: If you see a known issue that can cause your error, try to test with a previous version of RestSharp or an alpha version.
I hope you can now further investigate and debug the problem.
(I had similar problems and resolved it successful with this way of investigating and debugging)

WPF redirect to a page with POST request body

I have a simple WPF application with a button. When I click the button, I want to open the URL in the default browser and pass some data through POST body.
I know how to do that with GET, but what I want is using POST method.
Example:
Uri uri = new Uri("http://mypage.com");
string data = somedata;
// what to do next to redirect with POST method?
Note: I want to open the page in the user's default browser but not the .NET browser control.
Try using Process.Start(uri); it will detect the protocol (http) and open the default app for that protocol which will be your browser.
But that will result in a GET. Maby its better to use a WebRequest or RestAPI.

C# Console Application:Execute Javascript from HttpWebResponse String

I am making a program for using console application,I get response text about html page string,now I want to Inject some javascript code for httpwebresponse and execute it.thanks.
[Execute string in C# Console App] my real requirement is to click OK button then to redirect a new url ,then to get one of the param of the full url.

Facebook push notification

Im using Facebook C# SDK to post and fetch data. These simple tasks took forever to get running properly. Now I need to notify my webpage(ASP.NET C#) about changes on the specific facebook page so the webpage knows then it should fetch new data.
I have looked at this page : https://developers.facebook.com/docs/graph-api/real-time-updates/v2.2
But as usally with facebook documentations it misses to explain in detail how it works and how to get it working. Where exacly do I create the subscriptions? It says /{app-id}/subscriptions but I have tried this url with my app-id but no page is found?
I have tried to find examples on how to set this up but to no sucess.
Could someone please explain how this works? What do I need to do exacly to get this running?
Facebook subscription works by pinging a URL you own every time data has changed. You need to add a URL you own as a callback URL for Facebook subscriptions to work with
POST /v2.2/{app-id}/subscriptions HTTP/1.1
Host: graph.facebook.com
object=page
callback_url=http%3A%2F%2Fexample.com%2Fcallback%2F
fields=feed
verify_token=thisisaverifystring
In the above API request a POST request is made to add http://example.com/callback/ as the callback URL subscribing to the feed edges of the page object (a page object that the session user owns)
In your callback URL you must have it handle two actions
the initial callback (Handling Verification Requests via the verify token)
saving updated subscriptions (Receiving the Real Time Updates)
Here is an example of what it looks like in PHP
<?php
if ($_REQUEST['hub_verify_token'] === 'thisisaverifystring') {
echo $_REQUEST['hub_challenge'];
}
$file = 'sample.txt';
$inputJSON = json_decode(file_get_contents('php://input'));
file_put_contents($file, file_get_contents('php://input') );
?>

Sending ICalendar event to client while still continue running your code

I have built a basic calendar event using DDay.iCal, when I click "Add to calendar" link I produce an event and then sends this to the client.
Basically, my application works like this.
A User logs in.
Selects a specific date.
Books a specific timeslot
Clicks the "Add to calendar" link
Sending the event is done by using Response.Write() which sends the following to the client:
Response.ContentType = "text/calendar";
Response.AddHeader("Content-disposition", "attachment; filename=appointment.ics");
Response.Write(iCalString);
The above works fins but it requires me to first book the event then manually and then click the "Add to calendar" link.
I want to merge the steps 3 and 4. But when trying to do so the event booking gets saved to the database but the screen does not get refreshed.
Is there a "simple" way to get around this?
You need to set up a simple HttpHandler to post the reply and then do a Response.Redirect() into it.
I have done similar work before with integrating the vcard format into an our people section of a website. These articles should tell you everything you need to set up the HttpHandler - just replace the vcard code with your ical code.
http://professionalaspnet.com/archive/2007/09/23/Streaming-a-vCard-on-the-Fly-in-ASP.NET-with-a-Custom-httpHandler.aspx
http://weblogs.asp.net/gunnarpeipman/archive/2009/08/09/creating-vcard-with-image-in-net.aspx
When you redirect into the ical handler it will just instantly pop up the download box, you wont be taken to an empty page, the user is left on the same page.
Make sure that you clear your response (Response.Clear) prior to sending the iCal response information. Finally, end your response (Response.End) before any other content can be emitted. From the end use standpoint this will produce the same result of using an HttpHandler without the hassle.

Categories