HttpAPI Image


(Sistemas Logicos) #1

A service is being developed to upload images from an android application.

1-HttpAPI is used, the first attempt was made by passing the image in base64

[ServiceMethod]
[ApiMethod(HttpApiMethod = ApiRequestMethod.Post, HttpApiPath = “protected/UploadFile/{Client}/{File}/{Name}”, HttpApiTags = new[] { “service” })]

    public int UploadFile(string Client, string File,string Name)
    {
        try
        {

            int result = BLFile.Instance().UploadFile(Client, File, Name, Program.l_AppConf);
            //return result;

            return 1;
        }
        catch (ErrorException ex1)
        {
            throw new ApiMethodException(HttpStatusCode.NotFound, ex1.ReturnMessage);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            throw new ApiMethodException(HttpStatusCode.NotFound);
        }
    }

In the client when sending the image in base64 this error is received

The connection is terminated: A connection that was expected to remain active was closed by the server.

2-Second attempt using a byte array

        [ServiceMethod]
        [ApiMethod(HttpApiMethod = ApiRequestMethod.Post, HttpApiPath = "protected/UploadFileB/{Client}/{photoData}/{Name}", HttpApiTags = new[] { "service" })]

        public int UploadFileB(string Client, Binary photoData, string Name)
        {
            try
            {
              //Guardar imagen
                return 1;
            }
            catch (ErrorException ex1)
            {
                throw new ApiMethodException(HttpStatusCode.NotFound, ex1.ReturnMessage);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw new ApiMethodException(HttpStatusCode.NotFound);
            }
        }

Error: Invalid URI: the URI string is too long

please help.


(antonk) #3

Hello

You cannot use Uri template like "protected/UploadFileB/{Client}/{photoData}/{Name}" because photoData can be rather long thus the resulting Url will violate the Url length limitation (f.e. in Chrome one cannot use Urls containing more than 1038 chars)

This is the best approach to define this method:

	[ApiMethod(HttpApiMethod = ApiRequestMethod.Post, HttpApiPath = "protected/UploadImage/{client}/{name}", HttpApiOperationId = "UploadImage", HttpApiTags = new[] { "service" })]
	public void UploadImage(string client, Binary photoData, string name)

Note the HttpApiOperationId parameter - it allows Swagger CodeGen to generate prettier method names.

In this case the photoData parameter is expected to be passed in the request body as

{
    "photoData": "... Base64 encoded data here ..."
}

RO SDK will decode it for you into a sequence of bytes and will pack it into a stream (the Binary type is based on the System.IO.MemoryStream type).

The static method RemObjects.SDK.Helpers.StreamHelpers.StreamToBuffer would help you to copy the stream’s data into a byte array (there’s a lot of other helper methods too).


Btw please remember that if you use the Swagger-generated code to access the server then you might need to explicitly provide its Url in the client constructor. This is required because by default Swagger generates code that expects the server to use https connection so if your server uses plain http then the client won’t be able to connect to it.

Regards