0

I am using an API that is out of my control, as well having joined a development team recently that is using Retrofit1.

I am unable to send the request to the server in the format required, as the server requires multipart form data Body in the following format:

Uniqueidentifier:FileName.jpg:ReroutServerIP:Base64EncodedDocString.

I have tried many different techniques in order to accomplish this task but I cannot find any working method to do this. The server tells me that the message format not supported. Here is my current code (with the url stripped out). Please could someone assist?

@POST("URL")
public Response post_SendData(@Header("Content-Type") String ContentType, @Body String body);

In order to achieve the desired result, I can use postman with no headers and post a file from my system using the form-data post method. In the working postman post, the Key is the formatted string mentioned above and the value is a file selected from my desktop. Please see below for postman (edited to remove urls).

Postman

Thanks a lot guys.

1 Answers1

0

If you want to send a file to the server :

  public void uploadPictureRetrofit(File file, Callback<YourObject> response) {


        // this will build full path of API url where we want to send data.
        RestAdapter restAdapter = new RestAdapter
                .Builder()
                .setEndpoint(YOUR_BASE_URL)
                .setConverter(new SimpleXMLConverter()) // if the response is an xml
                .setLogLevel(RestAdapter.LogLevel.FULL)
                .build();

        // SubmitAPI is name of our interface which will send data to server.
        SendMediaApiInterface api = restAdapter.
                create(SendMediaApiInterface.class);
        TypedFile typedFile = new TypedFile(MULTIPART_FORM_DATA, file);

        api.sendImage(typedFile, response);
    }

And this is the interface SendMediaApiInterface :

public interface SendMediaApiInterface {
    @Multipart
    @POST("url")
    void sendImage(@Part("here_is_the_attribute_name_in_webservice") TypedFile attachments, Callback<YourObject> response);}
Imene Noomene
  • 3,035
  • 5
  • 18
  • 34
  • I think this is close to what I need, the only issue being that the @Part name needs to be specified each time, as the unique identifier is unique to the user. Do you know of any way that I could do this? – George Leslie Oct 19 '17 at 13:06
  • for example if **here_is_the_attribute_name_in_webservice** = **"image_attribute"** Your api url will be like this : http://base_ur_example.com/upload_photo_example?image_attribute= Please check your api and fill the corresponding attribut – Imene Noomene Oct 19 '17 at 13:13
  • I have edited the post, perhaps I am asking the question wrong? – George Leslie Oct 19 '17 at 13:27