I'm creating a WPF desktop client for work, and need to get data from a website using it's API.
This websites API requires a POST request in order to get data instead of a GET. This is a security feature as far as I can tell.
I cannot figure out how to format my request as most documentation is for application/json requests, but I need to POST my request using application/x-www-form-urlencoded .
The API will then respond in JSON format with the requested data which I will need to parse and use.
Here is what the websites limited documentation says:
API Request Format Our REST API is based on open standards, so you can use any web development language to access the API.
To make a request, you'll make an HTTPS POST to the applicable endpoint URL.
For example: https://app.agencybloc.com/api/v1/individuals/search
No parameters should be in the URL or querystring. The body of the request must contain the security credentials, as well as the method-specific parameters.
Note: The responses from the API are in JSON format as described in the method descriptions, but you do not POST JSON-formatted requests. They must be in application/x-www-form-urlencoded format per the above.
Here is what I need to POST to the site:
Host: https://app.agencybloc.com/api/v1/individuals/search
Cache-Control: no-cache
Content-Type: application/x-www-form-urlencoded
sid=mySID&key=myKey&updatedDTM=09/20/2021
And here is my code so far.
public class tdate
{
public string updatedDTM { get; set; }
}
public class APIHelper
{
public void Main(string[] args)
{
HttpClient client = new HttpClient();
HttpContent content = new FormUrlEncodedContent(
new List<KeyValuePair<string, string>>()
);
content.Headers.ContentType = new MediaTypeHeaderValue("application/x--www-form-urlencoded");
content.Headers.ContentType.CharSet = "UTF-8";
content = "sid=mySID&key=myKey&updatedDTM=09/20/2021";
client.DefaultRequestHeaders.ExpectContinue = false;
client.BaseAddress = new Uri(https://app.agencybloc.com/api/v1/individuals/search/);
}
}
I feel like I'm way off here. I have no clue how to even finish the POST request properly. I can't seem to find any definitive documentation, and none of the other questions about this topic make sense to me (as a relative newb in C#).
for the rest of the project I will have to parse the JSON response and make a second POST request using that data for even more data.
Let me know if I need to provide any more of my code, or any other details.
Thank you for any help.
EDIT: Got this to work.
Here's the Method:
public static HttpResponseMessage BlocCall()
{
HttpClient client = new HttpClient();
var dict = new Dictionary<string, string>();
dict.Add("sid", "MyID");
dict.Add("key", "MyKey");
dict.Add("lastName", "Heine");
var req = new HttpRequestMessage(HttpMethod.Post, "https://app.agencybloc.com/api/v1/individuals/search/") { Content = new FormUrlEncodedContent(dict) };
var res = client.SendAsync(req).Result;
Newtonsoft.Json.JsonConvert.SerializeObject(res, Formatting.Indented);
Trace.WriteLine(res);
return res;
}
And here is where it is called on Button Click:
private void Button_Click(object sender, RoutedEventArgs e)
{
APIHelper.BlocCall();
}
CodePudding user response:
You can check something similar to this.
HttpClient client = new HttpClient();
var dict = new Dictionary<string, string>();
dict.Add("sid", "mySID");
dict.Add("key", "myKey");
dict.Add("updatedDTM", "09/20/2021");
var req = new HttpRequestMessage(HttpMethod.Post, "https://app.agencybloc.com/api/v1/individuals/search/") { Content = new FormUrlEncodedContent(dict) };
var res = client.SendAsync(req).Result;