0

I ran a API request thorough postman and it ran successfully and this is its HTTP

POST //dummyendpoint/ HTTP/1.1
Host: dummyhost.com
Authorization: token="dummy_token"
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW

----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="config_enroll_settings"; filename="/C:/Users/SaurabhKumar/Desktop/config_enroll_settings.xml"
Content-Type: text/xml

(data)
----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="usagePolicy"; filename="/C:/Users/SaurabhKumar/Desktop/usagePolicy.xml"
Content-Type: text/xml

(data)
----WebKitFormBoundary7MA4YWxkTrZu0gW

But then I am trying to achieve the same through JAXRS implementation I am getting 500 response code and when I checked the server side logs it said couldnt determine MIME boundary

This is my JAXRS interface I have explicitly added boundary but still it dosent works

@Consumes("multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW")
@POST
@Path("/dummy_endpoint/{billingId}/")
public Object configureDeviceEnrollSettings(
        @PathParam("billingId") String billingId,
        @Multipart("multipartBody") MultipartBody multipartBody,
        @HeaderParam("Authorization") String authorizationHeaderValue);

This is how I am calling this interface

Attachment configSettingsFile = new Attachment("config_enroll_settings", "multipart/form-data", new File("/C:/Users/SaurabhKumar/Desktop/config_enroll_settings.xml"));
Attachment usagePolicyFile = new Attachment("usagePolicy", "multipart/form-data", new File("/C:/Users/SaurabhKumar/Desktop/usagePolicy.xml"))

/* 
I have tried this way also This way it resulted in exception 

No serializer found for class java.io.FileDescriptor and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain:
 org.apache.cxf.jaxrs.ext.multipart.MultipartBody["childAttachments"]
->java.util.AbstractList$SubList[0]
->org.apache.cxf.jaxrs.ext.multipart.Attachment["dataHandler"]
->javax.activation.DataHandler["dataSource"]
->org.apache.cxf.jaxrs.ext.multipart.InputStreamDataSource["inputStream"]
->java.io.FileInputStream["fd"])


File file1 = new File("/C:/Users/SaurabhKumar/Desktop/config_enroll_settings.xml");
ContentDisposition cd1 = new ContentDisposition("form-data; name=\"config_enroll_settings\"; filename=\"/C:/Users/SaurabhKumar/Desktop/config_enroll_settings.xml\"");
Attachment configSettingsFile = new Attachment("config_enroll_settings", new FileInputStream(file1), cd1);

File file2 =new File("/C:/Users/SaurabhKumar/Desktop/usagePolicy.xml");
ContentDisposition cd2 = new ContentDisposition("form-data; name=\"config_enroll_settings\"; filename=\"/C:/Users/SaurabhKumar/Desktop/usagePolicy.xml\"");
Attachment usagePolicyFile = new Attachment("usagePolicy", new FileInputStream(file2), cd2);

*/


List<Attachment> list=new LinkedList<>();
list.add(configSettingsFile);
list.add(usagePolicyFile);

MultipartBody body = new MultipartBody(list);

Object object = accountResource.configureDeviceEnrollSettings(billingId, body, "token=" + "\"" + authToken + "\"");

here is the stackTrace

com.webservices.utilities.httpclient.http.rest.ApiException: status 500 reading AccountResourceClient#configureDeviceEnrollSettings(String,MultipartBody,String)
    at com.webservices.utilities.httpclient.http.rest.ApiErrorDecoder.decode(ApiErrorDecoder.java:42)
    at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:149)
    at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:78)
    at feign.ReflectiveFeign$FeignInvocationHandler.invoke(ReflectiveFeign.java:103)
    at com.sun.proxy.$Proxy16.configureDeviceEnrollSettings(Unknown Source)
    at com.webservices.settings.ImportSettingsRoutine.importConfig(ImportSettingsRoutine.java:103)
    at com.webservices.abstractclasses.AbstractRoutine.executeRoutine(AbstractRoutine.java:14)
    at com.webservices.clients.ImportClient.executeRoutineQueue(ImportClient.java:47)
    at com.webservices.Main.main(Main.java:69)

here is the raw request from the jaxrs client

POST https:/dummyendpoint/ HTTP/1.1
Accept: application/xml
Authorization: token="dummy_token"
Content-Length: 1861
Content-Type: multipart/form-data
User-Agent: ApiClient-1.0
X-FL-APPNAME: ApiClient
X-FL-REQ-ID: dumm_id

{
  "type" : {
    "type" : "multipart",
    "subtype" : "related",
    "parameters" : { },
    "wildcardType" : false,
    "wildcardSubtype" : false
  },
  "rootAttachment" : {
    "headers" : {
      "Content-ID" : [ "config_enroll_settings" ],
      "Content-Type" : [ "text/xml" ]
    },
    "object" : "C:\\Users\\SaurabhKumar\\Desktop\\config_enroll_settings.xml",
    "contentType" : {
      "type" : "text",
      "subtype" : "xml",
      "parameters" : { },
      "wildcardType" : false,
      "wildcardSubtype" : false
    },
    "contentId" : "config_enroll_settings"
  },
  "allAttachments" : [ {
    "headers" : {
      "Content-ID" : [ "config_enroll_settings" ],
      "Content-Type" : [ "text/xml" ]
    },
    "object" : "C:\\Users\\SaurabhKumar\\Desktop\\config_enroll_settings.xml",
    "contentType" : {
      "type" : "text",
      "subtype" : "xml",
      "parameters" : { },
      "wildcardType" : false,
      "wildcardSubtype" : false
    },
    "contentId" : "config_enroll_settings"
  }, {
    "headers" : {
      "Content-ID" : [ "usagePolicy" ],
      "Content-Type" : [ "text/xml" ]
    },
    "object" : "C:\\Users\\SaurabhKumar\\Desktop\\usagePolicy.xml",
    "contentType" : {
      "type" : "text",
      "subtype" : "xml",
      "parameters" : { },
      "wildcardType" : false,
      "wildcardSubtype" : false
    },
    "contentId" : "usagePolicy"
  } ],
  "childAttachments" : [ {
    "headers" : {
      "Content-ID" : [ "usagePolicy" ],
      "Content-Type" : [ "text/xml" ]
    },
    "object" : "C:\\Users\\SaurabhKumar\\Desktop\\usagePolicy.xml",
    "contentType" : {
      "type" : "text",
      "subtype" : "xml",
      "parameters" : { },
      "wildcardType" : false,
      "wildcardSubtype" : false
    },
    "contentId" : "usagePolicy"
  } ]
}
Saurabh Kumar
  • 437
  • 1
  • 5
  • 18
  • 1
    I don't use CXF but here are the things that look wrong to me: 1) You should not have `@Multipart("multipartBody")`. You use the annotation to extract single attachments. But the `MultipartBody` is used to get the entire multipart entity. So remove the annotation. 2) It should be `@Consumes("multipart/form-data")` without the boundary. 3) When you make the `Attachment`, the type should not be `multipart/form-data`. The type is `text/xml`. 4) You need a `@Produces`. 5) You should not use `Object` as the return type. You need a clearly defined type so the client knows what reader to use. – Paul Samsotha Mar 08 '21 at 18:47
  • still 500 response code – Saurabh Kumar Mar 09 '21 at 03:58
  • Do you have a stacktrace? If not try to use an ExceptionMapper to print the stacktrace. [See](https://stackoverflow.com/a/31289875/2587435) – Paul Samsotha Mar 09 '21 at 04:52
  • I will try doing that Thanks !! – Saurabh Kumar Mar 09 '21 at 04:59
  • Also did you inspect the raw request? Is it correct (in format and content)? – Paul Samsotha Mar 09 '21 at 05:04
  • I tried doing that with fiddler but it failed to catch the https requests from jaxrs some certifications not trusted. I added the stackTrace – Saurabh Kumar Mar 09 '21 at 05:05
  • That is a server side exception or client side? – Paul Samsotha Mar 09 '21 at 05:08
  • client Side, I will have to look for server side logs, I will update when I find them. – Saurabh Kumar Mar 09 '21 at 05:10
  • I don't think the request is even going out. If it's a client side exception, there will be no request made. If it's a server side error, the request will still be made and you will get a response with a 500. But you do not have a response. Try to [use the regular WebClient](https://cxf.apache.org/docs/jax-rs-multiparts.html#JAX-RSMultiparts-UploadingfileswithClientAPI) to make the request and see what happens. – Paul Samsotha Mar 09 '21 at 05:12
  • Also the ExceptionMapper is supposed to be used on the server side, not client side. – Paul Samsotha Mar 09 '21 at 05:14
  • Are you trying to use Feign client with your JAX-RS interface to make the request? – Paul Samsotha Mar 09 '21 at 05:17
  • yes its a feign client wrapper, i will try and get the logs first – Saurabh Kumar Mar 09 '21 at 05:18
  • sorry I am an intern I think it will be really tough to get server side creds for seeing the logs. I have updated the raw request from the client, I tried running the request through okhttp client as well from the postman code it runs fine then also. – Saurabh Kumar Mar 09 '21 at 06:43
  • I don’t think it will work with the feign client the way you are using it. You are treating the jax-rs interface as if it were a feign-style interface which it is not. The content-type header says multiparty/form-data, yet the data is send as JSON (which is totally wrong). You will not be able to use the Jax-rs interface with feign. You either need to either use a new feign-style interface or use the cxf (Jax-rs) client. I linked the documentation for the cxf client call. Try it with that. – Paul Samsotha Mar 09 '21 at 08:21
  • 1
    Also if you don’t have access to logs you can always send the stacktrace as the response body (if there is an exception). There are many ways to find out information without logs. You just need to send the information in the response someway – Paul Samsotha Mar 09 '21 at 08:23
  • maybe I can use this jaxrs interface using feign client because it works for the GET request for fetching the data works like this only, I am not too sure about the request which is getting built up I think thats where the problem lies, Thanks for helping though :) – Saurabh Kumar Mar 09 '21 at 17:55
  • 1
    Ok so I never used the Feign client before and I just got a chance to play around with it. So basically when you set the encode and decoder the Jackson, it can only handle the conversion of JSON request and responses. It does not know how to do anything with multipart. I would imagine that you would need to create a custom encoder if you want the client to be able to send multipart. I found [this](https://github.com/OpenFeign/feign-form) though. It might help you with multipart. – Paul Samsotha Mar 10 '21 at 12:18
  • okay Thank you sooooo much I think this was the problem since I read the API only accepts xml and jackson handles only json requests encoding and decoding. so bumm I have to use ok-http client for this API endpoint. would be happy to accept this as answer :) – Saurabh Kumar Mar 10 '21 at 19:05

0 Answers0