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.
I believe, you will need to include boundary to your payload.
:ContentType("multipart/form-data; boundary=some_really_hard_to_match_string")
Yes. I have used boundary. I missed to include it in above code. Apologies.
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.
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?
Thank you. I will try that way as well.
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).
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.