1

So I have to make a method in asp.net for an API that accepts 2 files via PUT (1 json and 1 xml for processing data, not saving- because I have to, OK? :) ), sending the request via fiddler..

Right now I'm sending the request like this on fiddler (PUT METHOD):

Content-Type: multipart/form-data
Authorization: XXXXX 
Host: XXXX

Request body:

<@INCLUDE *C:\Users\ZZZZ.json*@>
<@INCLUDE *C:\Users\XXXX.xml*@>

Here's what I've tried inside the controller so far:

[HttpPut, Route("api/Student/{studentId}/Classes/{classId}")]
public async Task<string> Put(int studentId, int classId)
{
        var file = HttpContext.Current.Request.Files.Count > 0 ?
    HttpContext.Current.Request.Files[0] : null;
    Stream fileContent = await this.Request.Content.ReadAsStreamAsync();
    MediaTypeHeaderValue contentTypeHeader = this.Request.Content.Headers.ContentType;


    if (fileContent != null)
        return "ok";
    return "not ok";
}

So far the file is not being uploaded nor appears within the request (everything's null). I've also tried the "Request" variable and HttpContext.

Tried the exact same thing but with a POST Method (including the boundaries) and the same happens.

What would you do in order to make this work? I really have to send a json object and another in xml, I really can't change languages or send everything in json ('cause that I could make it work)...

PS: The files don't have a defined structure, it has to be dynamic PS2 : How would you then attempt to read those files without actually saving them?

1 Answer 1

1

You don't have to use a stream to read the file content. You can just try using the HttpPostedFile.

  [HttpPut, Route("api/student/{studentId}/classes/{classId}")]
    public async Task<string> Put(int studentId, int classId)
    {
        if (HttpContext.Current.Request.Files.Count == 0)
            throw new HttpResponseException(new HttpResponseMessage()
            {
                ReasonPhrase = "Files are required",
                StatusCode = HttpStatusCode.BadRequest
            });

        foreach (string file in HttpContext.Current.Request.Files)
        {
            var postedFile = HttpContext.Current.Request.Files[file];
            if (!(postedFile.ContentType == "application/json" || postedFile.ContentType == "application/xml"))
            {
                throw new System.Web.Http.HttpResponseException(new HttpResponseMessage()
                {
                    ReasonPhrase = "Wrong content type",
                    StatusCode = HttpStatusCode.BadRequest
                });
            }

        }
        return "OK";
    }

POSTMAN

hot

My POSTMAN:

enter image description here

Fiddler enter image description here

Sign up to request clarification or add additional context in comments.

9 Comments

Thanks man, but that didn't work, as I said in question, I've tried the HttpContext. In this case, "HttpContext.Current.Request.Files.Count" is zero.
I updated the post with a POSTMAN screenshot. Make sure your files are being sent by using the postman example above. You can download postman for free, just do a google search. The issue might be with your file upload.
I'm trying to use postman, but the problem is I can't seem to get it right. I always get a 403 forbidden error, but I got my client certificate and authorization and impersonate the same as I have in fiddler (and in fiddler it works). Anyways, trying your code with POST (if it's a post the files aren't empty. But that foreach only reads the first file, json (and it's all messed up), so the second time the string is empty. Could you please help?
Is it possible for you to test in local development without an authorization? In any case, check out my fiddler 4 screenshot. I changed the code just a bit so only application/json and application/xml are allowed. To get the Request Body Syntax in the composer, I had to upload both files at the same time. Otherwise, the Fiddler just reads the content of the file and adds it to the body and does not simulate a file upload.
The thing is fiddler only lets u upload 2 files if it's a POST. Well, being a POST is kinda working out for me already, just gotta figure out how to parse that xml cause it's not a well formatted xml (eg : starting tag is <client> )
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.