D
D
Duster2015-08-16 14:22:32
Java
Duster, 2015-08-16 14:22:32

How to generate a POST request to VKontakte API?

Hello dear.
Can you please tell me how to make a normal multipart/form-data request to the VKontakte API?
I will make a reservation right away, you need to transfer not files, but large lists.
Tried to pass the usual application/x-www-form-urlencoded - does not accept it.
Tried like this:

try {
      URL url = new URL("https://api.vk.com/method/groups.getById");

      HttpURLConnection connection = (HttpURLConnection) url.openConnection();
      connection.setDoOutput(true);
      connection.setRequestMethod("POST");

      MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.STRICT);
      multipartEntity.addPart("", new StringBody("group_ids=1,2,3,4"));
      // и так multipartEntity.addPart("group_ids", new StringBody("1,2,3,4"));

      connection.setRequestProperty("Content-Type", multipartEntity.getContentType().getValue());

      try (OutputStream out = connection.getOutputStream();
           InputStream is = connection.getInputStream();
           BufferedReader rd = new BufferedReader(new InputStreamReader(is))) {
        multipartEntity.writeTo(out);

        String line;
        StringBuffer response = new StringBuffer();
        while ((line = rd.readLine()) != null) {
          response.append(line);
        }

        return response.toString();

      }

    } catch (Exception e) {
      log.error("", e);
    }

used
<dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpmime</artifactId>
            <version>4.2.4</version>
        </dependency>

Does not work. I always catch error 100:
{
    "error":
    {
      "error_code":100,
      "error_msg":"One of the parameters specified was missing or invalid: group_ids is undefined",
      "request_params":
      [
        {
          "key":"oauth",
          "value":"1"
        },
        {
          "key":"method",
          "value":"groups.getById"
        }
      ]
    }
  }

Tell me how is it right?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Duster, 2015-08-17
@Duster

I don’t know why it didn’t work yesterday, apparently, I messed up with the logic.
Everything works as usual.

public static void main(String[] args) throws Exception {
    final String url = "https://api.vk.com/method/groups.getById";
    final String ids = "group_ids=1,2,3,4";

    URL obj = new URL(url);
    HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();

    //add reuqest header
    con.setRequestMethod("POST");
    con.setRequestProperty("User-Agent", "VKontakte/1.0");
    con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");


    // Send post request
    con.setDoOutput(true);
    DataOutputStream wr = new DataOutputStream(con.getOutputStream());
    wr.writeBytes(ids);
    wr.flush();
    wr.close();

    int responseCode = con.getResponseCode();
    System.out.println("Sending 'POST' request to URL : " + url);
    System.out.println("Post parameters : " + ids);
    System.out.println("Response Code : " + responseCode);

    BufferedReader in = new BufferedReader(
        new InputStreamReader(con.getInputStream()));
    String inputLine;
    StringBuffer response = new StringBuffer();

    while ((inputLine = in.readLine()) != null) {
      response.append(inputLine);
    }
    in.close();

    //print result
    System.out.println(response.toString());
  }

G
GavriKos, 2015-08-16
@GavriKos

Apparently you need json in groups. Those.
multipartEntity.addPart("group_ids", new StringBody("[1,2,3,4]"));
Well, use a sniffer to check what you are sending...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question