Forum Discussion

richie's avatar
richie
Community Hero
2 years ago

REST Request multipart/form-data Query Parms Issues - Nested Json Attributes

Hi,

 

I was investigating another post someone else has submitted (about submitting a REST request with multipart/form-data type) and almost the same issue popped up in my current work and I've finally remembered how to submit these types of requests with attached files and request payloads - but I've stumbled across a couple of issues that I can't work out how to get around.

 

So - I have a POST REST request that includes a request payload as well as a file attachment, so the MediaType needs to be either multipart/form-data or multipart/mixed.  My endpoint is expecting multipart/form-data.

 

The instructions indicate you attach the file (e.g. a letter.docx file) and then this attached file is associated to a 'file' query parameter on the REST request and check the 'Post QueryString' checkbox

e.g.

 

 

In my scenario I also have a request payload (in json format) and so the way to handle adding a request payload to a multipart/form-data request is rather than paste in the json payload into the editable field for request payload, instead you create a query parameter - one for each of the name/value pairs that comprise the json payload.

e.g.

 

 

In the above Request - you can see all the Query Parm attributes that actually comprise the name/value pair attributes that exist within the json Request payload.

 

The POST's request payload is as follows:

 

{
  "name": "Joe Bloggs",
  "email": "joe@example.org",
  "interestedParty": true,
  "ipReference": "3287498209",
  "deadline": "Deadline 3",
  "submissionType": "Navigation Document/Guide to the application",
  "sensitiveData": true,
  "lateSubmission": false,
  "submissionId": 10809,
  "representation": "This is my representation",
  "file": {
    "name": "My-Submission-Document-10809.pdf",
    "originalName": "My Submission Document.pdf",
    "size": 226022,
    "md5": "b1946ac92492d2347c6235b4d2611184"
  },
  "caseReference": "EN010118",
  "dateSubmitted": "2022-08-02T08:24:05.000Z"
}

 

 

I have 3 problems that I don't know how to handle.

 

PROBLEM 1:

When you attach a file to a REST request, you add in a 'file' query parm which is associated to the attached file, HOWEVER - the json request payload also includes a 'file' name/value pair attribute, so I need to add in a second 'file' query parm which maps to the request payload's file attribute, but I can't add a second 'file' query parm into the listing because the names of the query parms need to be unique.

 

Can anyone advise how I can circumvent this issue? - i.e. I need to be able to include a file query parm that corresponds to the request payload's json attribute as well as handle using the 'file' query parm to attach a file to the REST request.

 

PROBLEM 2:

I have nested json attributes within my request payload - i.e. the 'file' item in my request payload is actually a json object which contains further json attributes - 'name', 'originalName', 'size' and 'md5'.  Does anyone know how I can add in these nested attributes (JSONPATH for these attributes is $.file.name, $.file.originalName, $.file.size, and $.file.md5).  On top of this issue, if you look at the content - we have duplicate named attribute in the payload.  We have 2 'name' attributes - one 'name' has JSONPATH $.name, the second 'name' has JSONPATH $.file.name

 

Can anyone advise how I can add in query parms that correspond to nested json payload attributes?

 

PROBLEM 3:

There is some cross field validation on the query parm - if the "representation" attribute is present in payload (as a query parm), then the "file" query parm (that is part of the request payload) should NOT be included in the request.  But as we already identified above - I need the file query parm present (I can't set the parm TYPE from QUERY to PLAIN (to remove the attribute), cos then the attached file won't be picked up.

 

I've got the Open API Spec defining all the APIs for the project open in Swagger Editor and I used Fiddler to sniff the traffic to see the RAW content of the requests, so I've been using that to tailor my changes in ReadyAPI to match - so at this point, I can in fact test the multipart/form-data POST request using Swagger Editor, but I want all my API tests within one tool - not spread across multiple tools, so I'm eager to get the above problems resolved, so all my testing resides within a ReadyAPI project.

 

I'm at a bit of a standstill so any help here would be great!

 

Apologies for the lengthy post - I just wanted to be clear and outline everything properly

 

Cheers!

 

rich

  • richie's avatar
    richie
    Community Hero
    Hi,

    Just an update. There's a small defect in ReadyAPI when you're adding attachment files to a REST request that steers you in the wrong direction. It's only that i noticed when i compared the instructions to what was actually happening when i noticed the disparity.

    When you click to add attach files to a REST request, the attached file gets mapped to a "file" QUERY parm. The value that populates this field needs tlo be the filename of the file attachment. I.e. "letter.docx".

    So! Click to add a file, browse to the relevant file, the last step you are offered if you wish to cache the file. Once this dialogue is closed, there is a row in the attachment tab that is autopopulated with the full path to the file (in Name column), Content-Type, Size, there's Type column and a ContentID column. All these are auto populated.
    The ContentID field was autopopulated with the filename....i.e. letter.docx. HOWEVER, this causes your requests to fail. The ContentID field needs to contain the value of the QUERY parm, it's mapped to (i.e. in my case "file"). It doesnt need the name of the file because the Name column includes the full path to the attachment file.

    Ok. That's it. Just wanted to highlight a Something to watch out for!


    Cheers!

    Rich