K
K
kosyak_472021-05-30 11:29:50
API
kosyak_47, 2021-05-30 11:29:50

Vkontakte API. Where to add "file" field in POST request?

Good afternoon. I am writing a program in c#, which is going to upload information about products in VKontakte. This is implemented through the Vkontakte API.

In general, I got to the point with uploading a photo for a product, where I had a problem. I will give a description of the implementation from the Vkontakte API site:
"Upload the file to the upload_url address obtained in the previous paragraph by generating a POST request with the file field . This field must contain an image in the multipart/form-data format."

The problem is that I can't understand, to the best of my inexperience, where to write the file field . I would be very grateful to anyone who can suggest a solution. Thanks

PS When sending a request with any content of the postedData field, the server returns a response in the form
"{"error":"ERR_UPLOAD_BAD_IMAGE_SIZE: market photo min size 400x400","bwact":"do_add","server":234031,"_sig":"9d38b3a8d7071e818f1ae2b4033ddcc8"}".

In general, the problem here is precisely the absence of the file field in the POST request. POST request

code:

public static HttpWebResponse PostMethodForUpload(string postedData, string postUrl)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(postUrl);

            request.Method = "POST";
            request.Credentials = CredentialCache.DefaultCredentials;

            UTF8Encoding encoding = new UTF8Encoding();
            var bytes = encoding.GetBytes(postedData);

            request.ContentType = "multipart/form-data";
            request.ContentLength = bytes.Length;

            using (var newStream = request.GetRequestStream())
            {
                newStream.Write(bytes, 0, bytes.Length);
                newStream.Close();
            }
            return (HttpWebResponse)request.GetResponse();
        }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
dgash2201, 2021-05-30
@kosyak_47

Can be done with WebClient
var wc = new WebClient();
var response = Encoding.ASCII.GetString(wc.UploadFile(uploadUrl, /* Path to file */ @"image.png"));

B
BasiC2k, 2021-05-30
@BasiC2k

https://m.habr.com/ru/post/511114/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question