Guerrilla
Guerrilla

Reputation: 14886

Put file to URL with Http Utils as multipart form encoded

Is it possible to PUT a file with Http Utils as multipart form encoded?

This is what I tried:

var response = $"{_baseUrl}{address}".PutBytesToUrl(File.ReadAllBytes(filePath), "image/jpeg", "*/*",
    requestFilter: req =>
    {
        req.Headers["x-aws-acl"] = "private";
        req.Headers["content_type"] = "image/jpeg";
        req.Headers["X-Shopify-Access-Token"] = _accessToken;
    });

The request goes through with 200 but the API (Shopify) doesn't have the image.

I tried running the request in postman and with postman the request works and shopify has the image after.

I used webhook.site to see what the different was in http utils and postman and it seems postman is sending multipart encoded form.

Here are http utils headers being sent that result in no image:

enter image description here

Here are postman headers:

enter image description here

Any way to get http utils to send the image as multipart form data?

Upvotes: 1

Views: 52

Answers (1)

mythz
mythz

Reputation: 143339

To Upload Files as multipart/form-data you would need to use the UploadFile APIs which accepts an overload for specifying which HTTP Method to use, e.g:

var webReq = (HttpWebRequest)WebRequest.Create("http://example.org/upload");
webReq.Accept = MimeTypes.Json;
using var stream = uploadFile.OpenRead();
webReq.UploadFile(stream, uploadFile.Name, MimeTypes.GetMimeType(uploadFile.Name), 
    method:"PUT");

Upvotes: 1

Related Questions