webMethods

 View Only

 Sending an XML file using pub.client:http

Kash Kash's profile image
Kash Kash posted Fri January 10, 2025 03:52 AM
Version - webMethods 10.15
 
I have gone through multiple posts regarding the same where the requirement is to send a CSV or PDF or JSON files to an HTTP server using pub.mime and pub.client:http. My requirement is to push an XML file in the same way, and I have tried to replicate the solution shared in these posts but unable to get 200 OK response.
 
Postman POC:
 
My Implementation so far:
Steps:
1. Taking the XML string and converting to XMLStream using pub.io.stringToStream
2. Writing the XMLStream from step 1 to a file in FS.
3. pub.mime:createMimeData:
      - mimeHeader:
             Content-Type -> multipart/form-data
      - subType -> form-data
4. pub.mime:addBodyPart
      - mimeData  -> mimeData
      - xmlStream from step 1 -> Content
      - mimeHeader:
              Content-Disposition  ->  form-data; name="file"; filename="/apps/data/stage/Trial.xml" (absolute path of the file in FS)
      - contenttype -> application/xml
5. pub.mime.getEnvelopeStream:
6. calling pub.client:http with envStream mapped to data.mimeStream
              
I have tried to directly use the XMLStream to create the mimeData (Without writing to a file) where in Content-Disposition, filename was not included. Getting the same issue. But the ideal solution shouldn't require the XML string to be written to a file.
 
I have also included all the required headers such as content-type -> multipart/form-data.
 
I am not sure if I need to handle this differently for an XML file. 
Appreciate your inputs on this.
 
Thanks
Kailash Kumar Mishra's profile image
Kailash Kumar Mishra

Hi Kash,

In case you haven't tried this already can you try below and see if it makes any difference

In Step 4, update header contenttype  from application/xml to text/xml

Rupinder Singh's profile image
Rupinder Singh

Do you need to send it as an MIME attachment ? Or are you just using this because you have this as an example ?

Kash Kash's profile image
Kash Kash

Hi @Kailash Kumar Mishra,

Thanks for your response.

Tried with text/xml instead of application/xml. Still getting the same response.

Hi @Rupinder Singh,

Thanks for responding.

I am trying to send this file as Mime attachment as I cannot see any other way to push a file to HTTP server using pub.client:http.

Please do let me know if there is an alternate way to send an XML file in HTTP body.

Rupinder Singh's profile image
Rupinder Singh

You dont need to do anything special. The only step that is needed is pub.client:http. In the inputs, specify the UR, the method would be POST and the XML string would be mapped to the input string under the data document.

 

Kash Kash's profile image
Kash Kash

Hi @Rupinder Singh,

The API can only accept multipart/form-data and we cannot send the XML in the request body as Content-Type = application/xml.

It has to be a file being sent as shown in the postman POC screenshot.

Chirag Sanghavi's profile image
Chirag Sanghavi

Bit unsual actually to require form-data.

Having said that -  if it works from Postman, try generating java code from postman and inspect it and check if that works?

Akshith Arremreddy's profile image
Akshith Arremreddy

Try this,

  1. Convert XML string to stream.
  2. Create empty Mime data object.
  3. Add the stream from step 1 to MIME body using the service pub.mime:addBodyPart. You need to pass the mimeData object from step 2 to input for this service  (implicit map since variable name same) along with contenttype set to "application/xml" and multipart to yes.
  4. Create a MIME stream using pub.mime:getEnvelopeStream and pass in the mimeData doc as input to this service from step 3.
  5. Call pub.client:http with envStream from step 4 to "data/mimeStream" input. Set the http header "Content-Type" to "text/xml". Make sure you use the http method as POST instead of GET while calling the service.

Technically this should send the MIMEStream as Multipart message instead of in the http body.

Kash Kash's profile image
Kash Kash

Hi @Chirag Sanghavi,

Thanks for your input.

Postman can only generate OkHTTP or Unirest Java codes which are not compatible on our server.

I have tried to replicate the same by using HttpsUrlConnection but looks like the URL is not reachable as it is not using the proxy bypass. 

Kash Kash's profile image
Kash Kash

Hi @Akshith Arremreddy,

Thanks for the input.

The server is only expecting Content-Type = multipart/form-data; bounday=<boundary>.

We cannot send application/xml or text/xml to it.

Kash Kash's profile image
Kash Kash

The issue is resolved now.

Solution:

Instead of passing the envStream generated by pub.mime:getEnvelopeStream to the data.mimeStream of pub.client:http,

I am converting the envStream to String and passign it to data.String of pub.client:http.

Along with Content-Type as multipart/form-data; boundary=<boundary>.

Thanks for all the inputs.