Sending an image via http request

  1. 2 years ago

    henry_cho

    8 Mar 2018 User since 2018

    Hi,
    I love the product, I'm sorry if this is a basic question. I'm trying to send an image from my app to a service. I can load images from the camera roll and I can get the camera to take new photos but I'm not sure how I reference the image in an http request to send the image to my api.
    Any help would be appreciated

  2. marco

    8 Mar 2018 Administrator User since 2016

    Hi @henry_cho,
    in which format your api expects the image?

  3. henry_cho

    8 Mar 2018 User since 2018

    Hi Marco, jpg or PNG are both fine thanks!

  4. marco

    9 Mar 2018 Administrator User since 2016

    Once you have the image reference you can call:

    var data = JPEGRepresentation(value);

    or

    var data = PNGRepresentation();

    to get binary data.

    More information in the Image class doc .
    Even if I don't think that your API expects binary data, I think that data should be encoded in some way.

  5. last year

    henry_cho

    22 Apr 2018 User since 2018

    Hi Marco,
    Thanks for your previous help.
    I'm trying to set up oauth2 for imgur.
    I've set up my keys for the api and everything works well while using postman, but when I try it in the app I get error messages.
    I've been reading your blog post and it mentions adding the "client ID" for the simulator?
    Sorry if this is obvious but any help would be appreciated

  6. marco

    23 Apr 2018 Administrator User since 2016

    @henry_cho which error messages are you receiving?
    Can you please send more details?

  7. henry_cho

    25 Apr 2018 User since 2018

    Marco,
    I get this error from the server
    {"data":{"error":"Imgur is temporarily over capacity. Please try again later."},"success":false,"status":500}
    I get this message consistently when making the request from creo but not from postman.
    I've looked up this message and it seems to be related to problems with authentication.
    When i read the creo blog articles i saw mention of different parameters which seem unclear including client id for simulator.

  8. henry_cho

    25 Apr 2018 User since 2018

    Hi Marco I seem to have resolved the connection issue.
    As I mentioned before I'm trying to create a simple app that lets me take a photo and upload it to IMGUR using their api.
    I've now got the api connected and can fetch images.
    In my app I let the user either choose a photo or take a photo to upload.
    When they select a photo from a carousel I have this code in the cameraroll1 object under the new image event:
    var ImageData = image.PNGRepresentation();
    Imgur.upload.image = ImageData;
    Imgur.upload.run();

    In my request object(upload) under my http client(imgur) I have a post request that has an Image value in the body.
    I'm not sure how it should be encoded.
    When I run the prototype I get this error

    Unable to find image into class NSMutableData

    Any help would be appreciated.

  9. marco

    26 Apr 2018 Administrator User since 2016

    Hi @henry_cho can you please send me the project?

  10. henry_cho

    26 Apr 2018 User since 2018

    Thanks Marco

  11. andrea

    26 Apr 2018 Administrator User since 2016

    Hi @henry_cho, you just need to set the Imgur.upload.image property with a string value, in this case a Base64 encoded value from the image data as specified by the API provider. So your code must be: Imgur.upload.image = ImageData.base64();
    Please see the attached file, I've also added some code to the NewValue event of the request object (upload) to print the response from imgur to the console.

  12. henry_cho

    26 Apr 2018 User since 2018

    Thanks so much !

  13. henry_cho

    26 Apr 2018 User since 2018

    @andrea I'm still getting errors when i run this?
    An integer index or index range is required to access string items.
    Unable to find image into class _upload

  14. andrea

    27 Apr 2018 Administrator User since 2016

    Hi @henry_cho, the errors are caused by a bug in the current version of Creo, a new version that will fix this bug (and many other improvements) will be released next week.

    Meanwhile, I've added a workaround to your project in the NewImage event of the CameraRoll1 to avoid the double invocation that causes the error, please see the attached file.

  15. henry_cho

    28 Apr 2018 User since 2018

    Thanks @andrea , it seems quite fragile. As soon as i change the start page back to my start page I start getting errors again.
    Will this be fixed in the next release?
    Cheers,
    Henry

  16. cjp160

    19 Nov 2018 User since 2018

    @andrea is there a way to decode from base64 string back to image data? I want to be able to receive an image via HTTP request and display that image in an ImageView

  17. marco

    20 Nov 2018 Administrator User since 2016

    @cjp160 in the next upcoming version we have extended the Crypto class with methods for encoding/decoding base64 strings to string/data. In your case you'll use:

    var data = Crypto.decodeDataFromBase64(base64String);
    vat image = Image(data, 1.0);

or Sign Up to reply!