3 Replies Latest reply on Oct 3, 2017 12:21 AM by gregu777

    Upload a XLS file to jive group PS/Curl/Postman

    gregu777

      Hello everyone,

      after 2 days of trying doing this myself, i think i will have to give up as nothing worked so far for me. I have read most of the discussions here, as well as other portals.

      I wanted to upload a file from my local drive to our company jive , where my team has a group.

      Here 'Upload a local file using the Jive API '

      it was explained how to do it with curl, not sure what i am doing wrong, but this is not working for me. Also not sure if somebody managed to do it, as it seems that json is not valid ( tags section is missing  " " ).

      Even if i put "" for the tags section, it still does not want to work. I tried to do this with postman but i suppose that i send bad data there :/

      Has anyone tried a successful attempt to do it using powershell/curl/postman ? Or maybe is there a document that describes what/how has to be sent ?

      On the picture below it is : 'conetnt' , i already changed it to content, but still the same.

      Update---

      I actually made it running with curl, all i changed was the : "tags": "d", to : "tags: [api_upload]  . I do not understand, why this is correct form, any ideas ?Has anyone made it work using powershell ?

      Greg

        • Re: Upload a XLS file to jive group PS/Curl/Postman
          gregu777

          So i was digging a bit more, i have understood why there is [  ], that just represents array.

          Curl example is working ok, now i need to somehow do it using powershell. Invoke-webrequest can not support multipart, i already so on internet example of those calls, so now i need to try to do it.

          Right now what i don't understand in curl example is the -F ....;type=application/json  , -F ...;type=text/xml   . I tried to make calls to httpbin.org/post to see what my curl example send, and i did not see them, any idea what are those for ?

          • Re: Upload a XLS file to jive group PS/Curl/Postman
            gregu777

            For all of those that can't use curl on linux, while running it from windows:

            curl -k -u user:pass https://site/api/core/v3/contents -F "json=@C:\\jsonPayload.json;type=application/json" -F "file=@c:\\1testfile.txt;type=text/xml" -X POST -v -i

             

            This will work just fine. Now back to powershell

            • Re: Upload a XLS file to jive group PS/Curl/Postman
              gregu777

              Ok, i got it working managed to find all i needed in the web.

              https://www.snip2code.com/Snippet/396726/PowerShell-V3-Multipart-formdata-example

              So looking at this example, i adjusted it to call our jive . Tested it twice, and it worked just fine.

              I will leave the code in case somebody will be looking for this.

               

              $FilePath='C:\test\curl\12testfile.txt'

              $CODEPAGE = "iso-8859-1" # alternatives are ASCII, UTF-8

              # Read file byte-by-byte

              $fileBin = [System.IO.File]::ReadAllBytes($FilePath)

              # Convert byte-array to string

              $enc = [System.Text.Encoding]::GetEncoding($CODEPAGE)

              $fileEnc = $enc.GetString($fileBin)

              # Read a second hardcoded file which we want to upload through the API call

              $importConfigFileEnc = $enc.GetString([System.IO.File]::ReadAllBytes("C:\test\curl\1jsonPayload.json"))

              # Create Object for Credentials

              $user = 'someuser'

              $pass = ‘somepass’

              $pair = "$($user):$($pass)"

              $encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))

              $basicAuthValue = "Basic $encodedCreds"

              $Headers = @{'Authorization' = $basicAuthValue}

              $boundary = [System.Guid]::NewGuid().ToString()

              $LF = "`r`n"

              # Build up URI for the API-call

              $uri = 'https://site.com/api/core/v3/places/31500/contents'

              # Build Body for our form-data manually since PS does not support multipart/form-data out of the box

                  $bodyLines = (

                      "--$boundary",

                      "Content-Disposition: form-data; name=`"file`"",

              "Content-Type: text/xml$LF",

                      $fileEnc,

                      "--$boundary",

                      "Content-Disposition: form-data; name=`"json`"",

              "Content-Type: application/json$LF",

                      $importConfigFileEnc,

                      "--$boundary--$LF"

                   ) -join $LF

               

               

              PS C:\Users\gregu> Invoke-RestMethod -Uri $uri -Method Post -ContentType "multipart/form-data; boundary=$boundary" -Body $bodyLines -headers $Headers

              Invoke-RestMethod -Uri $uri -Method Post -ContentType "multipart/form-data; boundary=$boundary" -Body $bodyLines -headers $Headers

               

               

              payload file contents:

              {

                "authorship": "author",

                "type": "file",

                "visibility": "place",

                "parent": "https://site.com/api/core/v3/places/31500",

                "subject": "111This is my uploaded test file43",

                "tags": [api_upload],

                "content": {

                  "type": "text/html",

                  "text": "11T1his is my description for the upload file",

                  "name" : "12testfile.txt",

                  "size" : "14"

                }

              }

               

              Obviously, you can make it way better/prettier, but i was just trying to make it working.