Forum Discussion

saurabhsharma09's avatar
saurabhsharma09
Contributor
10 years ago
Solved

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 values in response.(As explained by the developer )
Ex:- Actual value:- Content-Type: image/png; name=2014-05-12_1220.png
Expected value:- Content-Type: image/png;
As i have not put name in Request attachment Content-Type ,I think it is getting appended automatically.
Is there any way to remove "name=2014-05-12_1220.png" from the Content-Type?
I am guessing this has to be done before request is submitted (through some groovy code in event handlers but not sure.)
Request is POST request with Media Type= multipart/form-data and Content-Disposition: form-data; in payload.
I've also added Attachment and Raw Request screenshot.

please help me in getting this resolved?

Regards,
Saurabh
  • 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

8 Replies

  • SmartBear_Suppo's avatar
    SmartBear_Suppo
    SmartBear Alumni (Retired)
    Hello,

    Unfortunately this is a bug that we are working on fixing (SOAP-1850). A workaround is to remove the attachments from the attachment tab and specify the full path of the files including the extensions in the parameters. Let me know if you have further issues or questions.

    Regards,
    Temil
  • hello,

    I have set resource root as ${projectDir} and i am not attaching files manually and using groovy script instead.
    And i've tried manually also by putting complete file path along with extension as suggested but no success.

    Are you planning to fix this defect anytime soon?

    Thanks,
    Saurabh
  • SmartBear_Suppo's avatar
    SmartBear_Suppo
    SmartBear Alumni (Retired)
    Hi,

    Set the parameter value to "file:filename"
    Set the media-type of the request body to "multipart/form-data" (available in the dropdown)
    Select the "Post QueryString" checkbox

    Check the attached image.

    Thanks,
    Jeshtha
    • ajain1981's avatar
      ajain1981
      New Member

      Hi Support,

       

      Is this bug resolved that adds name feild with Content-Type in attachment ?

       

      I have tried this solution by adding parameter value to "file:filename" ,but in this way it is taking wrong content-type for that attachment ,In my case I am attaching a JSON file but it is showing wrong Content-Type text/javascript which should be application/json. it looks like again a issue. there is no option to change Content-Type of attach file part request for multipart.

      Please suggest if is there any way arround

       

      Thanks

      Abhishek

  • msochon's avatar
    msochon
    New Contributor

    Hi,

    Is this bug resolved? 
    I've tried the suggested solutions,but sadly, they didn't work, my content-type is still being overwritten. Without the solution for this problem I can't continue with my work. If there's any other solution for this problem, I would by thankful for the tip.

    Thanks,
    Magda

    • anastasiia's avatar
      anastasiia
      New Contributor

      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