Clarity Service Management

Expand all | Collapse all

Upload attachment SDM API REST returns BadRequest

Jump to Best Answer
  • 1.  Upload attachment SDM API REST returns BadRequest

    Posted 02-07-2019 05:16 PM

    I'm following the the Java sample code for uploading an attachment to SDM API (version 17.1) via REST, now, using Postman, but continue to receive the cryptic BadRequest response with no idea of what is "bad" with my request. If anyone can help me understand what I'm missing, it would be greatly appreciated.

     

    Here is what I'm sending via Postman:

    --*****MessageBoundary*****
    Content-Disposition: form-data; name="payload"
    Content-Type: application/xml; CHARACTERSET=UTF-8

    <attmnt><repository id="1002"></repository><orig_file_name>test.txt</orig_file_name><attmnt_name>test.txt</attmnt_name><description>Upload using REST</description></attmnt>

    --*****MessageBoundary*****
    Content-Disposition: form-data; name="test.txt"; filename="test.txt"
    Content-Type: application/octet-stream

    7B68747470733A2F2F73646D73746167652E75776D65646963696E652E6F72673A383035312F63616973642D726573742F63723F7265706F7369746F727949643D31303032267365727665724E616D653D4956542D53444D322E414D432E55574D45444943494E452E4F5247266D696D65547970653D54657874266465736372697074696F6E3D4E6F6E6272616E64656453697465732E6373767D

    --*****MessageBoundary*****--

     

    Here's the header info:

    POST /caisd-rest/attmnt?repositoryId=1002&serverName=ivs-sdm2&mimeType=Text&description=test.txt HTTP/1.1
    Host: sdm.someserver.org:8051
    X-AccessKey: 30769721
    Content-Type: multipart/form-data; BOUNDARY=*****MessageBoundary*****
    Cache-Control: no-cache



  • 2.  Re: Upload attachment SDM API REST returns BadRequest

    Posted 02-11-2019 10:04 AM

    Hi,

    it seems , that your problem is related to the way, how postman is sending the raw data, especially regarding carriage return and linefeed.

    multipart form data boundaries do have some some special requirements. Postman sends the  raw data only using a linefeed character "\n" and ommit the carriage return "\r", which is required. That leads to an invalid request on the server side, because the server expects both "\r\n"

    Unfortunately , I wasn't able to get postman working until now. 

    But at least I was able to get SOAP-UI running , using ${="\r"} at the end of lines.

    Best regards

    .....Michael



  • 3.  Re: Upload attachment SDM API REST returns BadRequest

    Posted 02-11-2019 02:24 PM

    Michael,

     

    Thanks for the response! Very helpful. You said in your post that your weren't able to get Postman running until now. You got it to work? What did you use for the CRLF characters?



  • 4.  Re: Upload attachment SDM API REST returns BadRequest
    Best Answer

    Posted 02-12-2019 03:04 AM

    I got it,.... finally.

    its a bit tricky in postman.

    you can specify variables in postman.

    I set a variable for the "\r" character in a pre-request script:

    and then used this variable in the body wherever I thought it is necessary :

    Regards

    .............Michael



  • 5.  Re: Upload attachment SDM API REST returns BadRequest

    Posted 03-26-2019 09:29 AM

    Thanks a lot Michael !!! 

     

    Worked for me with "Headers" section:

    KEY                       VALUE 
    X-AccessKey         237219108                                                                                              <----- generated from any REST client tool
    Content-Type         multipart/form-data; BOUNDARY=*****MessageBoundary***** 



  • 6.  Re: Upload attachment SDM API REST returns BadRequest

    Posted 03-26-2019 10:38 AM

    Great that it works.

    Sure, you need to provide your own access key.

    Thanks for the feedback, Karen.

     

    Btw. on Linux the command "base64" can be used to encode and decode any kind of data. another reason for me to prefer this platform, even if its a very, very small and simple one.

    Even simpler than to use "openssl enc -base64 -e -in ...".



  • 7.  Re: Upload attachment SDM API REST returns BadRequest

    Posted 03-26-2019 10:51 AM

    Yes, access key always needed, and content type which specifies the boundary text in this case.

     

    Thank you for the Linux command, I'll try it out ...



  • 8.  Re: Upload attachment SDM API REST returns BadRequest

    Posted 03-22-2019 09:19 AM
      |   view attached

    Hi.

    I did some further research on this question, and want to share my thoughts and findings.

    If you want to upload binary data, like an Image or so, and want to use postman as the tool, you run into the following questions:

    1. the http multipart standard expects the use of CRLF (\r\n) as a separator between multipart parts in general as well as CRLF as a separator between part specific Header and part data. To my expirience, if you only use LF as a seperator SDM is not able to handle this request.
    2. the binary part headers must inlcude the "Content-Transfer-Encoding" Header , otherwise SDM is not able to correctly decode the given binary data, and will return an error.

     

    These general expectations of the SDM side, is fully compliant to the standard htttp definitions as far as I am aware.

     

    postman on the client side seems to have the following issues:

    1. When using the raw body to send out a POST request, you are using some kind of a text editor to enter the body data. This data can only be ASCII characters (more or less) and not real binary data (Byte values from 0 to 255).
      1. that means, when you want to send binary data, you have to encode this data somehow that way, that the data is represented as ASCII data, to be able to use it in postman. Base64 is such an Encoding.
      2. here the second requirement from above comes in place. You Need to tell the receiving system, how the data is encoded, so it can be successfully decoded again.
    2. when using the form capability of postman , to send the POST request , you are able to specify a file form field, which results in inserting the bytes of the file unencoded in the multipart request.
      1. Unfortunately, postman misses to add the "Content-Transfer-Encoding" Header. So again, SDM returns an error, because it can not know, how the data is encoded( in this case it is just not encoded, anyhow, even this needs to be told to the receiver).

     

    So reflecting all this, I was finally able to upload an image with postman. Of course, I needed to encode my Image data to base64. I'll attach a postman collection for your convinience.

     

    Hope this helps and clarifies the Scenario.

    Best regards.

    .....Michael

    Attachment(s)



  • 9.  Re: Upload attachment SDM API REST returns BadRequest

    Posted 03-25-2019 08:59 AM

    I used this online tool to convert my own .gif image to base64:

    https://www.browserling.com/tools/image-to-base64