简体   繁体   中英

Web API .NET MVC - Http POST - Fiddler request send string variable

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM