Reputation: 245
i am trying to make a httpwebrequest, to connect with a api of a customer. This works for most of the apis, but one of them requires ContentType = "multipart/form-data", which why i have to add it to my request. But since i added it, i get an System.Net.WebException at the line where i am using the GetResponse-method. My JsonRequest is correct, because i tried it at a demo site, but i do not know what to do with this Exception! If i do not at the ContentType multipart/form-data, i get an error msg from the api, that "not all parameters are set correctly". I hope you can help me!
var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.ContentType = "text/json";
httpWebRequest.Method = requestMethode;
httpWebRequest.Accept = "application/json";
httpWebRequest.ContentType = "multipart/form-data";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
}
string strResponse = "";
try
{
using (WebResponse httpResponse = httpWebRequest.GetResponse()) // error here
{
using (Stream responseStream = httpResponse.GetResponseStream())
{
using (StreamReader streamReader = new StreamReader(responseStream))
{
strResponse = streamReader.ReadToEnd();
}
}
}
}
catch (WebException exception)
{
throw exception;
}
JObject jobject = JObject.Parse(strResponse);
return jobject;
}
Thx a lot!
Upvotes: 0
Views: 1093
Reputation: 1039418
It looks like the remote endpoint that you are trying to call requires a multipart/form-data
encoded request. I strongly suggest you reading the corresponding specification
to better familiarize yourself with the exact payload that needs to be sent over the wire. This way you will better understand where the problem with your code is.
And once you have read the spec you could come up with the following:
string json = "PUT THE SAMPLE JSON TAKEN FROM HERE https://i.materialise.com/api/docs/cart-item-creation-api/";
var url = "https://imatsandbox.materialise.net/web-api/cartitems/register";
var request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
var boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x", NumberFormatInfo.InvariantInfo);
request.ContentType = "multipart/form-data; boundary=" + boundary;
boundary = "--" + boundary;
using (var stream = request.GetRequestStream())
using (var writer = new StreamWriter(stream))
{
writer.WriteLine(boundary);
writer.WriteLine("Content-Disposition: form-data; name=\"data\"; filename=\"blob\"");
writer.WriteLine("Content-Type: application/json");
writer.WriteLine();
writer.Write(json);
writer.WriteLine();
writer.WriteLine(boundary + "--");
}
using (var response = request.GetResponse())
using (var stream = response.GetResponseStream())
using (var reader = new StreamReader(stream))
{
string strResponse = reader.ReadToEnd();
return JObject.Parse(strResponse);
}
Also notice that I have removed the meaningless try/catch
in which you were simply rethrowing.
Upvotes: 2