Post JPEG file using fiddler with other body data

5.6k views Asked by At

I'm trying to post a jpeg file to a locally developed web service via Fiddler. This would be simple enough, but I also need to include some data alongside the file and can’t quite nail the syntax that fiddler wants. If I click the upload file button and select a file to upload, it replaces my POST body with:

---------------------------acebdf13572468
Content-Disposition: form-data; name="fieldNameHere"; filename="PantheraLeo.jpg"
Content-Type: image/jpeg

<@INCLUDE *C:\temp\PantheraLeo.jpg*@>
---------------------------acebdf13572468—

Now I want to add some additional data:

user=1&album=2&photo=[OUTPUT FROM FILE UPLOAD]

I’ve tried putting this at the start of the body, but when my Node app receives the request, I’m getting a user parameter, an album parameter but no photo.

Any ideas on how I could format this request to get both parameters and the photo uploaded as the photo parameter?

2

There are 2 answers

2
Shogan On BEST ANSWER

I've also been looking to do something similar myself and stumbled on your question. I've just managed to achieve what I needed after a bit of messing about with Fiddler. Try this:

---------------------------acebdf13572468
Content-Disposition: form-data; name="model" 

MyModelInfo

---------------------------acebdf13572468
Content-Disposition: form-data; model="test123"; filename="123.gif"
Content-Type: image/gif

<@INCLUDE *Z:\Downloads\123.gif*@>
---------------------------acebdf13572468--

It would seem that you link the form data being sent up in your request body to the 'acebdf13572468' boundary in the POST info. Provide the Content-Disposition with a key name (in my case 'model') and then the following line represents your actual value for this key. (In my case "MyModelInfo".

Using the above request body I was able to POST up a file as well as some accompanying POST data to my API.

0
MattPil29 On

The accepted answer works well. But be warned the extra line after MyModelInfo comes through into the string. Also when copying and pasting in and out of fiddler some lines were corrupted breaking the file.

Note I have named the file param "file" in the fiddler body and in the receiving API function.

This works for me:

---------------------------acebdf13572468
Content-Disposition: form-data; name="PARAM1"

Some text with a line before but not after
---------------------------acebdf13572468
Content-Disposition: form-data; name="file"; filename="filename.jpg"
Content-Type: image/jpeg

<@INCLUDE *C:\local\filename.jpg*@>
---------------------------acebdf13572468--

The data can be received in .net core 2 like this:

    [HttpPost]
    [Route("AddImage")]
    public async System.Threading.Tasks.Task<IActionResult> AddImageAsync(IFormFile file)
    {
        //f is the same as file
        //var f = Request.Form.Files.Count > 0 ? Request.Form.Files[0] : null;

        //useful to check the keys
        //var additionalProperties = Request.Form.Keys;

        if (file != null)
        {
            try
            {
                if (Request.Form.TryGetValue("PARAM1", out StringValues p1))
                {
                     var txt = p1.ToString():