I have a WindowsForm ADO.NET application that will use a post method call to a Flask api and upload an xml file
here is how i call the api using python :
import requests
API_URL = 'http://localhost:5000'
with open('result.xml') as fp:
content = fp.read()
response = requests.post(
'{}/files/result.xml'.format(API_URL), data=content
)
i've been looking on how to call the api with method post and uplaod a file in c# windows form
here is what i end up with :
the upload function :
private async Task<IRestResponse> UploadAsync(string fileName, string server)
{
var client = new RestClient(server);
var request = new RestRequest("/files", Method.POST);
XmlDocument doc = new XmlDocument();
// init XMLDocument and load customer in it
doc = new XmlDocument();
doc.Load(@""+fileName+"");
// Update (PUT) customer
request = new RestRequest("/files", Method.POST);
request.Parameters.Clear();
request.AddParameter("text/xml;charset=utf-8", doc.InnerXml, ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
return response;
}
the function call :
private async void button2_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(label1.Text))
{
string url = "http://localhost:5000";
IRestResponse restResponse = await UploadAsync(label1.Text,url);
if (restResponse.StatusCode == System.Net.HttpStatusCode.OK)
MessageBox.Show("You have successfully uploaded the file", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
clicking the button does not do anything no errors and it doesn't send the file
any idea on how can i fix this ?
to fix this problem i used this code :
XmlDocument doc = new XmlDocument();
doc = new XmlDocument();
doc.Load(@""+textBox1.Text+"");
var client = new RestClient("http://appapi9.azurewebsites.net/files/filethatillcheck.xml");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/xml");
request.AddParameter("application/xml",""+doc.OuterXml+"", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
MessageBox.Show("done");
i had to load the xml file and send it's content