Sylvia
Sylvia

Reputation: 2616

Uploading zip file works in curl, but not in Powershell - why?

I'm switching from regular files to zip files doing an upload, and was told I'd need to use a header in this format - Content-Type: application/zip.

So, I can get my file to upload properly via curl with the following:

curl --verbose --header "Content-Type: application/zip" --data-binary @C:\Junk\test.zip "http://client.xyz.com/[email protected]&password=testpassword&job=test"

However, when I write a simple powershell script to do the same thing, I run into problems - the data isn't loaded. I don't know how to get a good error message returned, so I don't know the details, but bottom line the data isn't getting in.

$FullFileName = "C:\Junk\test.zip"
$wc = new-object System.Net.WebClient -Verbose
$wc.Headers.Add("Content-Type: application/zip")
$URL = "http://client.xyz.com/[email protected]&password=testpassword&job=test"
$wc.UploadFile( $URL, $FullFileName ) 
# $wc.UploadData( $URL, $FullFileName ) 

I've tried using UploadData instead of UploadFile, but that doesn't appear to work either.

Thanks, Sylvia

Upvotes: 2

Views: 9667

Answers (2)

Andy Arismendi
Andy Arismendi

Reputation: 52609

Now that I look at it again I think you need: $wc.Headers.Add("Content-Type", "application/zip") because the collection is key/value paired. Check out this SO question:

WebClient set headers

Also if your still having issues you might need to add a user agent header. I think curl has it's own.

$userAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2;)"
$wc.Headers.Add("user-agent", $userAgent)

Upvotes: 1

Jeffery Hicks
Jeffery Hicks

Reputation: 943

I don't necessarily have a solution but I think the issue is that you are trying to upload a binary file using the WebClient object. You most likely will need the UploadData method but I think you are going to have to run the zip file into an array of bytes to upload. That I'm not sure of off the top of my head.

If you haven't, be sure to look at the MSDS docs for this class and methods: http://msdn.microsoft.com/en-us/library/system.net.webclient_methods.aspx

Upvotes: 2

Related Questions