Reputation: 11972
I need to call an API with POST arguments, for example:
I've had a look at XDocument but not sure how to send params in the request. I'm also not sure how I would call asynchronously, and even if I should or if it's better/easier to run in another thread.
I'd be calling this from my windows based C# application.
Upvotes: 2
Views: 6418
Reputation: 63
var client = new RestClient("www.api.url");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader("id", "givenid");
request.AddHeader("HASH", "generatedHash");
request.AddParameter("text/plain", "fullxml or body", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
POST /api/somename/1.1
Host: google.com
id: merchantid
HASH: generatedshah
<node1><element>Individual</element></node1>
import http.client
import mimetypes
conn = http.client.HTTPSConnection("url")
payload = "<Node><Element>Individual</Element></Node>"
headers = {
'id': 'givenid',
'HASH': 'generatedhash'
}
conn.request("POST", "/api/blacklist/verify", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
Upvotes: 1
Reputation: 527
Call it from where? Javascript? If so you can use JQuery:
http://api.jquery.com/jQuery.post/
$.post('http://localhost/myAPI/', { options: "blue", type="car"}, function(data) {
$('.result').html(data);
});
data will contain the result of your post.
If from server-side you can use HttpWebRequest and write to it's stream with your params.
// Create a request using a URL that can receive a post.
WebRequest request = WebRequest.Create ("http://localhost/myAPI/");
// Set the Method property of the request to POST.
request.Method = "POST";
// Create POST data and convert it to a byte array.
string postData = "options=blue&type=car";
byte[] byteArray = Encoding.UTF8.GetBytes (postData);
// Set the ContentType property of the WebRequest.
request.ContentType = "application/x-www-form-urlencoded";
// Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length;
// Get the request stream.
Stream dataStream = request.GetRequestStream ();
// Write the data to the request stream.
dataStream.Write (byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close ();
// Get the response.
WebResponse response = request.GetResponse ();
// Display the status.
Console.WriteLine (((HttpWebResponse)response).StatusDescription);
// Get the stream containing content returned by the server.
dataStream = response.GetResponseStream ();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader (dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd ();
// Display the content.
Console.WriteLine (responseFromServer);
// Clean up the streams.
reader.Close ();
dataStream.Close ();
response.Close ();
Upvotes: 0
Reputation: 2468
To throw another option into the ring, you may want to consider using the PostAsync
method of HttpClient
in .NET 4.5. An admittedly untested stab:
HttpClient client = new HttpClient();
var task = client.PostAsync(string.Format("{0}{1}", "http://localhost/myAPI", "?options=blue&type=car"), null);
Car car = task.ContinueWith(
t =>
{
return t.Result.Content.ReadAsAsync<Car>();
}).Unwrap().Result;
Upvotes: 0
Reputation: 43077
You can use one of the Upload methods of WebClient.
WebClient client = new WebClient();
string response = client.UploadString(
"http://localhost/myAPI/?options=blue&type=car",
"POST data");
Upvotes: 3