So I'm doing an API where I have to send a huge json file and a name for a variable to the database. Currently am sending and reading the file with my code, and it's working fine.
Then thing is that I have to send a string for a service name (may have "/" characters, that's why I didn't put it as the file name, or query string.
I tried sending this request (HTTP POST) :
---------------------------32r23rfewfwfaedef
Content-Disposition: form-data; name="fieldNameHere"; filename="XXXXXX.json"
Content-Type: application/json
{"name":"test"}
<@INCLUDE *C:\....\XXXXXX.json*@>
---------------------------32r23rfewfwfaedef--
The thing is that I can't find that variable anywhere in my controller... My code is like this:
public HttpResponseMessage Post()
{
if (HttpContext.Current.Request.Files.Count != 1)
throw new HttpResponseException(new HttpResponseMessage()
{
ReasonPhrase = "One file is required, a json in order to create the Swagger.",
StatusCode = HttpStatusCode.BadRequest
});
SwaggerSaveModel model = new SwaggerSaveModel();
HttpPostedFile postedFile = HttpContext.Current.Request.Files[0];
using (System.IO.StreamReader myFile = new System.IO.StreamReader(postedFile.InputStream))
{
var XmlObj = new StreamReader(postedFile.InputStream).ReadToEnd();
model.SwaggerJson = XmlObj.ToString();
}
return Request.CreateResponse(HttpStatusCode.Created, "blabla");
}
The file reading is fine, but I can't find the "name" variable... I also can´t change that json file, because it's a swagger and it has to be used exactly as it is being sent.
Please help..
For future doubts like mine, I figured out how to do this. I'm sending my request like this:
parameter: FileName
And accessing on my controller like this:
HttpContext.Current.Request.Params["HTTP_PARAMETER"]
Then it returns "FileName"