Send an attachment file with a POST request

Posted by jpatel7 on 30-Nov-2017 12:56

I have a JSON object which I am sending in a POST request. It was working fine. But, now I need to send a text file as an attachment as well. I tried the following code:

lv-multiPartEntity = new MultipartEntity().
      lv-messagePart     = new MessagePart("application/json", lv-requestParameters).


      lv-messagePart:Headers:Put(
         HttpHeaderBuilder:Build(
            "Content-Disposition")
            :Value("form-data; name=" + quoter("dataRequest"))
            :Header).

/* ------------- I added this section ---------- */
      lv-messagePart:Headers:Put(
         HttpHeaderBuilder:Build(
            "Content-Disposition")
            :Value('attachment; filename="C:\Users\me\Desktop\test.txt"')
            :Header).
/* ---------------------------------------------- */

      lv-multiPartEntity:AddPart(lv-messagePart).
lv-multiPartEntity:Boundary = guid. lv-request = RequestBuilder :POST(lv-webServiceURL, lv-multiPartEntity) :AddHeader("Accept", "application/json") :AddHeader("Authorization", "Bearer " + lv-accessToken) :ContentType("multipart/form-data") :Request.

I am having an error saying "Invalid cast from OpenEdge.core.String to Progress.Json.ObjectModel.JsonObject. (12869) ".

I also tried to add this file as a different messagePart, but it didn't work. What am I missing?

Thanks in advance.

All Replies

Posted by jquerijero on 30-Nov-2017 13:16

I believe, you will need to include boundary to your payload.

:ContentType("multipart/form-data; boundary=some_really_hard_to_match_string")

Posted by jpatel7 on 30-Nov-2017 13:25

Yes. I have used boundary. I missed to include it in above code. Apologies.

Posted by jquerijero on 30-Nov-2017 14:12

Does your code insert the proper header at the beginning of each data segment and a footer at the end of everything.

Structurally speaking your payload should look something like this;

"--" + boundary // header

data

"--" + boundary // header

data

"--" + boundary + "--" // footer

Those "--" are required. You might just have to use Fiddler to see what it is being sent.

Posted by jpatel7 on 30-Nov-2017 15:08

I checked. I think Boundary property of multipartEntity takes care of it. But can we add two headers for the same messagePart? Nothing wrong with my code?

Posted by Peter Judge on 30-Nov-2017 16:02
You can add as many headers as you want for the message parts.
 
IF you want to see what’s being sent , set the logging-level to 5 and otherwise set up the log-manager. The client will dump a request-raw.txt into the session temp-dir (-T); you can see then exactly what’s being sent.
Posted by jpatel7 on 30-Nov-2017 16:14

Thank you. I will try that way as well.

Posted by jpatel7 on 30-Nov-2017 16:29

 The below code worked for me, but I really need to send an attachment, which gives the error!

lv-messagePart     = new MessagePart("application/octet-stream", new String(lv-fileLongChar)).
      
lv-multiPartEntity:AddPart(lv-messagePart).
Posted by Peter Judge on 01-Dec-2017 13:16

You can add as many headers as you want for the message parts.

IF you want to see what’s being sent , set the logging-level to 5 and otherwise set up the log-manager. The client will dump a request-raw.txt into the session temp-dir (-T); you can see then exactly what’s being sent.

This thread is closed