Google Drive Uploading multipart mime base64 encoded file w/powershell: malformed multipart body error












0















Token retrieval all figured out.



Uploading files to Google Drive.



Getting error



Here is the powershell command I'm using:



$response = Invoke-RestMethod -Uri "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart" -Method Post -Headers $uploadHeaders -Body $uploadBody



Here is the string for the variable $uploadBody





--314159265358979323846
Content-Type: application/json; charset=UTF-8

{
"description": "TestCSV",
"name": "test-emp.csv",
"originalFilename": "test-emp.csv"
}

--314159265358979323846
Content-Transfer-Encoding: base64
Content-Type: application/octet-stream


c0FNQWNjb3VudE5hbWUsRU1QX05VTQ0KYnNtaXRoLDc3Mzk5Mg0KbHNvbmcsMzM3MTEwDQpnc3Vkbyw2NDkwNTc
--314159265358979323846--



This fails - only changed the data file:




--314159265358979323846
Content-Type: application/json; charset=UTF-8

{
"description": "TestCSV",
"name": "csv3.csv",
"originalFilename": "csv3.csv"
}

--314159265358979323846
Content-Transfer-Encoding: base64
Content-Type: application/octet-stream


c2VxLGZpcnN0LGxhc3QNCjEsQW15LEJhcmtlcg0KMixMZWUsU2FsYXphcg0KMyxFc3RlbGxhLEtlbm5lZHkNCg
--314159265358979323846--



For completeness, here is the header IDictionary param for $uploadHeaders





Name Value
---- -----
Authorization Bearer ya29.GlxbBiz86xKlRnjzQY2z70vHtWfL4sr28BvX-OKio8PbcEfJ0l7tQgx9btNqMdanYXG_WzSD74WD_15903vn0mYO0G0uI8lez5g77Z_W0gKrIRh_s...
Content-Type multipart/related; boundary=314159265358979323846
Content-Length 400



I've read a great deal on this and have made sure there were two rn before payload. Any pointers would be greatly appreciated!



Thank you!










share|improve this question























  • Are you uploading a file? Just use the -InFile parameter. There is very little need to part your file manually anymore.

    – TheIncorrigible1
    Nov 21 '18 at 20:47













  • That worked! Thank you so much #TheIncorribible1 ! I have changed it as follows: <pre>

    – Citroen
    Nov 23 '18 at 5:40













  • That made the file ok. But the title is "Untitled" & I can't seem to change it. Ideas?

    – Citroen
    Nov 23 '18 at 5:49













  • Apologies, I did not post this sooner:

    – Citroen
    Jan 7 at 16:33
















0















Token retrieval all figured out.



Uploading files to Google Drive.



Getting error



Here is the powershell command I'm using:



$response = Invoke-RestMethod -Uri "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart" -Method Post -Headers $uploadHeaders -Body $uploadBody



Here is the string for the variable $uploadBody





--314159265358979323846
Content-Type: application/json; charset=UTF-8

{
"description": "TestCSV",
"name": "test-emp.csv",
"originalFilename": "test-emp.csv"
}

--314159265358979323846
Content-Transfer-Encoding: base64
Content-Type: application/octet-stream


c0FNQWNjb3VudE5hbWUsRU1QX05VTQ0KYnNtaXRoLDc3Mzk5Mg0KbHNvbmcsMzM3MTEwDQpnc3Vkbyw2NDkwNTc
--314159265358979323846--



This fails - only changed the data file:




--314159265358979323846
Content-Type: application/json; charset=UTF-8

{
"description": "TestCSV",
"name": "csv3.csv",
"originalFilename": "csv3.csv"
}

--314159265358979323846
Content-Transfer-Encoding: base64
Content-Type: application/octet-stream


c2VxLGZpcnN0LGxhc3QNCjEsQW15LEJhcmtlcg0KMixMZWUsU2FsYXphcg0KMyxFc3RlbGxhLEtlbm5lZHkNCg
--314159265358979323846--



For completeness, here is the header IDictionary param for $uploadHeaders





Name Value
---- -----
Authorization Bearer ya29.GlxbBiz86xKlRnjzQY2z70vHtWfL4sr28BvX-OKio8PbcEfJ0l7tQgx9btNqMdanYXG_WzSD74WD_15903vn0mYO0G0uI8lez5g77Z_W0gKrIRh_s...
Content-Type multipart/related; boundary=314159265358979323846
Content-Length 400



I've read a great deal on this and have made sure there were two rn before payload. Any pointers would be greatly appreciated!



Thank you!










share|improve this question























  • Are you uploading a file? Just use the -InFile parameter. There is very little need to part your file manually anymore.

    – TheIncorrigible1
    Nov 21 '18 at 20:47













  • That worked! Thank you so much #TheIncorribible1 ! I have changed it as follows: <pre>

    – Citroen
    Nov 23 '18 at 5:40













  • That made the file ok. But the title is "Untitled" & I can't seem to change it. Ideas?

    – Citroen
    Nov 23 '18 at 5:49













  • Apologies, I did not post this sooner:

    – Citroen
    Jan 7 at 16:33














0












0








0








Token retrieval all figured out.



Uploading files to Google Drive.



Getting error



Here is the powershell command I'm using:



$response = Invoke-RestMethod -Uri "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart" -Method Post -Headers $uploadHeaders -Body $uploadBody



Here is the string for the variable $uploadBody





--314159265358979323846
Content-Type: application/json; charset=UTF-8

{
"description": "TestCSV",
"name": "test-emp.csv",
"originalFilename": "test-emp.csv"
}

--314159265358979323846
Content-Transfer-Encoding: base64
Content-Type: application/octet-stream


c0FNQWNjb3VudE5hbWUsRU1QX05VTQ0KYnNtaXRoLDc3Mzk5Mg0KbHNvbmcsMzM3MTEwDQpnc3Vkbyw2NDkwNTc
--314159265358979323846--



This fails - only changed the data file:




--314159265358979323846
Content-Type: application/json; charset=UTF-8

{
"description": "TestCSV",
"name": "csv3.csv",
"originalFilename": "csv3.csv"
}

--314159265358979323846
Content-Transfer-Encoding: base64
Content-Type: application/octet-stream


c2VxLGZpcnN0LGxhc3QNCjEsQW15LEJhcmtlcg0KMixMZWUsU2FsYXphcg0KMyxFc3RlbGxhLEtlbm5lZHkNCg
--314159265358979323846--



For completeness, here is the header IDictionary param for $uploadHeaders





Name Value
---- -----
Authorization Bearer ya29.GlxbBiz86xKlRnjzQY2z70vHtWfL4sr28BvX-OKio8PbcEfJ0l7tQgx9btNqMdanYXG_WzSD74WD_15903vn0mYO0G0uI8lez5g77Z_W0gKrIRh_s...
Content-Type multipart/related; boundary=314159265358979323846
Content-Length 400



I've read a great deal on this and have made sure there were two rn before payload. Any pointers would be greatly appreciated!



Thank you!










share|improve this question














Token retrieval all figured out.



Uploading files to Google Drive.



Getting error



Here is the powershell command I'm using:



$response = Invoke-RestMethod -Uri "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart" -Method Post -Headers $uploadHeaders -Body $uploadBody



Here is the string for the variable $uploadBody





--314159265358979323846
Content-Type: application/json; charset=UTF-8

{
"description": "TestCSV",
"name": "test-emp.csv",
"originalFilename": "test-emp.csv"
}

--314159265358979323846
Content-Transfer-Encoding: base64
Content-Type: application/octet-stream


c0FNQWNjb3VudE5hbWUsRU1QX05VTQ0KYnNtaXRoLDc3Mzk5Mg0KbHNvbmcsMzM3MTEwDQpnc3Vkbyw2NDkwNTc
--314159265358979323846--



This fails - only changed the data file:




--314159265358979323846
Content-Type: application/json; charset=UTF-8

{
"description": "TestCSV",
"name": "csv3.csv",
"originalFilename": "csv3.csv"
}

--314159265358979323846
Content-Transfer-Encoding: base64
Content-Type: application/octet-stream


c2VxLGZpcnN0LGxhc3QNCjEsQW15LEJhcmtlcg0KMixMZWUsU2FsYXphcg0KMyxFc3RlbGxhLEtlbm5lZHkNCg
--314159265358979323846--



For completeness, here is the header IDictionary param for $uploadHeaders





Name Value
---- -----
Authorization Bearer ya29.GlxbBiz86xKlRnjzQY2z70vHtWfL4sr28BvX-OKio8PbcEfJ0l7tQgx9btNqMdanYXG_WzSD74WD_15903vn0mYO0G0uI8lez5g77Z_W0gKrIRh_s...
Content-Type multipart/related; boundary=314159265358979323846
Content-Length 400



I've read a great deal on this and have made sure there were two rn before payload. Any pointers would be greatly appreciated!



Thank you!







powershell google-drive-sdk






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 20:23









CitroenCitroen

1




1













  • Are you uploading a file? Just use the -InFile parameter. There is very little need to part your file manually anymore.

    – TheIncorrigible1
    Nov 21 '18 at 20:47













  • That worked! Thank you so much #TheIncorribible1 ! I have changed it as follows: <pre>

    – Citroen
    Nov 23 '18 at 5:40













  • That made the file ok. But the title is "Untitled" & I can't seem to change it. Ideas?

    – Citroen
    Nov 23 '18 at 5:49













  • Apologies, I did not post this sooner:

    – Citroen
    Jan 7 at 16:33



















  • Are you uploading a file? Just use the -InFile parameter. There is very little need to part your file manually anymore.

    – TheIncorrigible1
    Nov 21 '18 at 20:47













  • That worked! Thank you so much #TheIncorribible1 ! I have changed it as follows: <pre>

    – Citroen
    Nov 23 '18 at 5:40













  • That made the file ok. But the title is "Untitled" & I can't seem to change it. Ideas?

    – Citroen
    Nov 23 '18 at 5:49













  • Apologies, I did not post this sooner:

    – Citroen
    Jan 7 at 16:33

















Are you uploading a file? Just use the -InFile parameter. There is very little need to part your file manually anymore.

– TheIncorrigible1
Nov 21 '18 at 20:47







Are you uploading a file? Just use the -InFile parameter. There is very little need to part your file manually anymore.

– TheIncorrigible1
Nov 21 '18 at 20:47















That worked! Thank you so much #TheIncorribible1 ! I have changed it as follows: <pre>

– Citroen
Nov 23 '18 at 5:40







That worked! Thank you so much #TheIncorribible1 ! I have changed it as follows: <pre>

– Citroen
Nov 23 '18 at 5:40















That made the file ok. But the title is "Untitled" & I can't seem to change it. Ideas?

– Citroen
Nov 23 '18 at 5:49







That made the file ok. But the title is "Untitled" & I can't seem to change it. Ideas?

– Citroen
Nov 23 '18 at 5:49















Apologies, I did not post this sooner:

– Citroen
Jan 7 at 16:33





Apologies, I did not post this sooner:

– Citroen
Jan 7 at 16:33












1 Answer
1






active

oldest

votes


















0














You have to rename the file after uploading it.



$sourceFile = 'C:tempallisv22018201810101009.csv'

$sourceItem = Get-Item $sourceFile

# If uploading to a Team Drive, set this to 'true'
$supportsTeamDrives = 'false'

# Set the file metadata
$uploadMetadata = @{
originalFilename=$sourceItem.Name
name=$sourceItem.Name
description="TestCSV"
}

# Set the upload headers
$uploadHeaders = @{
Authorization="Bearer $accessToken"
"Content-Type" = "application/json"
}

# Perform the upload
try {
$response = Invoke-RestMethod -Uri "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart" -Method Post -Headers $uploadHeaders -InFile $SourceFile
$urifile = "https://www.googleapis.com/drive/v3/files/$($response.id)"
$renameResponse = Invoke-RestMethod -Uri $urifile -Method Patch -Headers $uploadHeaders -Body '{ "name":"HappyFile.csv" }'


If you successfully upload your file, it will return the value in $urifile and you can use that to rename the file.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419961%2fgoogle-drive-uploading-multipart-mime-base64-encoded-file-w-powershell-malforme%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You have to rename the file after uploading it.



    $sourceFile = 'C:tempallisv22018201810101009.csv'

    $sourceItem = Get-Item $sourceFile

    # If uploading to a Team Drive, set this to 'true'
    $supportsTeamDrives = 'false'

    # Set the file metadata
    $uploadMetadata = @{
    originalFilename=$sourceItem.Name
    name=$sourceItem.Name
    description="TestCSV"
    }

    # Set the upload headers
    $uploadHeaders = @{
    Authorization="Bearer $accessToken"
    "Content-Type" = "application/json"
    }

    # Perform the upload
    try {
    $response = Invoke-RestMethod -Uri "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart" -Method Post -Headers $uploadHeaders -InFile $SourceFile
    $urifile = "https://www.googleapis.com/drive/v3/files/$($response.id)"
    $renameResponse = Invoke-RestMethod -Uri $urifile -Method Patch -Headers $uploadHeaders -Body '{ "name":"HappyFile.csv" }'


    If you successfully upload your file, it will return the value in $urifile and you can use that to rename the file.






    share|improve this answer




























      0














      You have to rename the file after uploading it.



      $sourceFile = 'C:tempallisv22018201810101009.csv'

      $sourceItem = Get-Item $sourceFile

      # If uploading to a Team Drive, set this to 'true'
      $supportsTeamDrives = 'false'

      # Set the file metadata
      $uploadMetadata = @{
      originalFilename=$sourceItem.Name
      name=$sourceItem.Name
      description="TestCSV"
      }

      # Set the upload headers
      $uploadHeaders = @{
      Authorization="Bearer $accessToken"
      "Content-Type" = "application/json"
      }

      # Perform the upload
      try {
      $response = Invoke-RestMethod -Uri "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart" -Method Post -Headers $uploadHeaders -InFile $SourceFile
      $urifile = "https://www.googleapis.com/drive/v3/files/$($response.id)"
      $renameResponse = Invoke-RestMethod -Uri $urifile -Method Patch -Headers $uploadHeaders -Body '{ "name":"HappyFile.csv" }'


      If you successfully upload your file, it will return the value in $urifile and you can use that to rename the file.






      share|improve this answer


























        0












        0








        0







        You have to rename the file after uploading it.



        $sourceFile = 'C:tempallisv22018201810101009.csv'

        $sourceItem = Get-Item $sourceFile

        # If uploading to a Team Drive, set this to 'true'
        $supportsTeamDrives = 'false'

        # Set the file metadata
        $uploadMetadata = @{
        originalFilename=$sourceItem.Name
        name=$sourceItem.Name
        description="TestCSV"
        }

        # Set the upload headers
        $uploadHeaders = @{
        Authorization="Bearer $accessToken"
        "Content-Type" = "application/json"
        }

        # Perform the upload
        try {
        $response = Invoke-RestMethod -Uri "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart" -Method Post -Headers $uploadHeaders -InFile $SourceFile
        $urifile = "https://www.googleapis.com/drive/v3/files/$($response.id)"
        $renameResponse = Invoke-RestMethod -Uri $urifile -Method Patch -Headers $uploadHeaders -Body '{ "name":"HappyFile.csv" }'


        If you successfully upload your file, it will return the value in $urifile and you can use that to rename the file.






        share|improve this answer













        You have to rename the file after uploading it.



        $sourceFile = 'C:tempallisv22018201810101009.csv'

        $sourceItem = Get-Item $sourceFile

        # If uploading to a Team Drive, set this to 'true'
        $supportsTeamDrives = 'false'

        # Set the file metadata
        $uploadMetadata = @{
        originalFilename=$sourceItem.Name
        name=$sourceItem.Name
        description="TestCSV"
        }

        # Set the upload headers
        $uploadHeaders = @{
        Authorization="Bearer $accessToken"
        "Content-Type" = "application/json"
        }

        # Perform the upload
        try {
        $response = Invoke-RestMethod -Uri "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart" -Method Post -Headers $uploadHeaders -InFile $SourceFile
        $urifile = "https://www.googleapis.com/drive/v3/files/$($response.id)"
        $renameResponse = Invoke-RestMethod -Uri $urifile -Method Patch -Headers $uploadHeaders -Body '{ "name":"HappyFile.csv" }'


        If you successfully upload your file, it will return the value in $urifile and you can use that to rename the file.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 7 at 16:40









        CitroenCitroen

        1




        1
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419961%2fgoogle-drive-uploading-multipart-mime-base64-encoded-file-w-powershell-malforme%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

            Alcedinidae

            Origin of the phrase “under your belt”?