Forum Discussion

saurabhsharma09's avatar
saurabhsharma09
Contributor
11 years ago

Update Content-Type in Attachment before submission

Hello, After I attach a file to REST request and send it successfully, I am seeing name also along with Content Type in Raw request tab and due to which i am not getting few expected parameter val...
  • anastasiia's avatar
    anastasiia
    6 years ago

    Hi everyone,

     

    I had a discussion with customer support regarding this issue, and they suggest the solution. 

     

    To specify your content type and remove name of the file after it, you should add event handler to the request. Steps are the next:

     

    1) Click on your project through the navigator then click on the "Events" button on the top right of the application (underneath the LoadUI tab)

    2) Click the green plus button to add a new event and then select the "RequestFilter.filterRequest" Event Handler

    3) Click on the RequestFilter (if it's not selected already) and copy+paste the below script into the box below:

    def bodyPart = context.getProperty("httpMethod").requestEntity.message.content.getBodyPart(0) // get a part by number, it starts with 0
    bodyPart.removeHeader("Content-Type") 
    bodyPart.addHeader("Content-Type", "application/content-type+xml") // specify the value without the name parameter


    The above script completely removes the Content-Type Header from the raw request and replaces it with only the desired content type (so in this case, application/content-type+xml).

    4) Afterwards, please run your request with the attachment (and with no parameters).

     

    Please pay attantion that this part 

    .getBodyPart(0)

     is used because I'm working with xml attachments, so for other content types script may differ.

     

    This solution resolved my problem, so I hope it will help you