Post JSON file programatically to System Folder: Converting Curl script to Swift code

Discussion in 'LiquidFiles General' started by rullrich, Jan 23, 2019.

  1. rullrich

    rullrich New Member

    Joined:
    Jan 23, 2019
    Messages:
    2
    Likes Received:
    0
    I'm working for a client that has an enterprise LiquidFiles account. I'm working from the Curl-based script that LiquidFiles uses as their example on how to post to a LiquidFiles System Folder, which I have working. Now I'm trying to convert this script to Swift code in an iOS app that I am developing, but I am having a devil of a time getting it working.

    I know this may be a bit obscure, but I can't imagine I'm the only one who's ever tried to do such a thing. Here's the working curl-based script, based on what LiquidFiles provided (and de-identified):

    -----
    #!/bin/bash

    # Some variables
    filedrop_server=https://filetransfer.organization.edu
    filedrop_url=$filedrop_server/filedrop/user-key

    # upload the file and get attachment id
    attachment_id=`curl -X POST -F Filedata=@fileToUpload.json --user "authorizationKey" $filedrop_server/attachments`

    echo $attachment_id

    cat <<EOF | curl -s -X POST -H "Content-Type: application/json" -d @- $filedrop_url
    {"message":
    {
    "from":"sender@sender.com",
    "subject":"Test Environment Event",
    "message":"Sample JSON-file for Test Environment Event",
    "attachments":["$attachment_id"]
    }
    }
    -----

    Enclosed below is the Swift code I currently have, where I'm just trying to do the code above that "upload the file and get the attachment id". I have been unsuccessful in getting even this to work whereby I get an attachment_id returned.

    The next block of code from the curl-based script seems to be posting an email with that attachment_id included, and sending it to the System Folder we have set-up, but I'm not sure if this should be coded in a HTTPS POST or sent as an email.

    I know that this is rather obscure, but if anyone has any ideas AT ALL on how I could get this working, I really appreciate the help.

    Regards, Randy

    -----

    Here is the Swift code in its current state (I'm using a popular framework called Alamofire to do the HTTP calls).

    let fileUploadHeaders = [
    "Authorization": "authorizationKey"
    ]

    do {
    let jsonData = try jsonEncoder.encode(eatEvent)
    Alamofire.upload(multipartFormData: { multipartFormData in
    multipartFormData.append(jsonData,
    withName: "Event",
    fileName: "fileToUpload.json",
    mimeType: "application/json")
    },
    to: "https://filetransfer.organization.edu/attachments",
    headers: fileUploadHeaders,

    encodingCompletion: { encodingResult in

    switch encodingResult {
    case .success(let upload, _, _):
    upload.validate()
    upload.responseJSON { response in
    guard response.result.isSuccess,
    let value = response.result.value else {
    print("Error while uploading file: \(String(describing: response.result.error))")
    return
    }

    // let attachmentID = JSON(value)["uploaded"][0]["id"].stringValue
    print(response.result.value)

    }
    case .failure(let encodingError):
    print(encodingError)
    }
    })
    } catch {
    print("Couldn't encode Meal as JSON in ForgotMeal flow.")
    }
    }
     
  2. David

    David Administrator
    Staff Member

    Joined:
    Dec 1, 2015
    Messages:
    781
    Likes Received:
    31
    The dropped file and FileDrop message is sent via HTTPS POST, the notification email you don't need to handle in the app. It's sent to the recipient of the FileDrop from the LF server automatically when the uploaded file is completely processed.

    In this case I can recommend to troubleshoot what exactly is posted from your App by using some development proxy.
    When you compare the results from the working curl script with results from the App, it should help troubleshoot what's necessary to change or add in the Alamofire app.
    For more details please check this development troubleshooting how to: https://man.liquidfiles.com/api/development.html
     
    rullrich likes this.
  3. rullrich

    rullrich New Member

    Joined:
    Jan 23, 2019
    Messages:
    2
    Likes Received:
    0
    Thanks, David, for your reply. With some help from a colleague, I got this all working very nicely and am able to upload a JSON file to a given system filedrop folder. The source code in the iOS Swift language, for anyone who may benefit from it, is attached and de-identified (broken into three functions to match the three functions from the example Curl script). This file is called "Working JSON Upload" (sorry it's not prettier; this forum wouldn't let me upload a Rich-Text-File; had to export to PDF).

    However, while I can upload a JSON file, I am now having trouble following the same pattern to upload a .jpg file. Please see the function: uploadFileWithKey(apiKey: String), which is the second function in the attached. You will see the block of code at the top of this function that successfully uploads the JSON.

    Then, beneath that, is a block of code that tries to do the exact same thing, but instead of uploading JSON, is trying to upload a .jpg file. But here's the error I am getting -- the "response.result.value" returned when I try to upload the .jpg comes back as "Upload Error, invalid parameters". I've (more than) triple checked what I think the parameters should be for an image file, and I can't figure out why this isn't working.

    Thanks for any help!
     

    Attached Files:

Share This Page