0

Trying to add Mangnet Link to a Premiumize Account using their API https://app.swaggerhub.com/apis-docs/premiumize.me/api/1.4#/transfer/transferCreate.

Response message: {"status":"error","message":"src is missing"}

Code is as follows:-

void pushMagnet(final String apiKey, final String magnetLink, final Context context) {
    final String url = "https://premiumize.me/api/transfer/create?apikey=" + apiKey;

    Map<String, String> magnetSrc = new HashMap<>();
    //Parameters i want to send
    magnetSrc.put("src", magnetLink);

    RequestQueue queue = Volley.newRequestQueue(context);

    JsonObjectRequest stringRequest = new JsonObjectRequest(Request.Method.POST, url,
            //here added as an JSON Object
            new JSONObject(magnetSrc),
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    Log.e("Rest Response:", response.toString());
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e("Rest Error:", error.toString());
        }
    }
    ) {
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> headers = new HashMap<>();
            headers.put("accept", "application/json");
            headers.put("Content-Type", "multipart/form-data");

            return headers;
        }
    };
    queue.add(stringRequest);
}

I've already tried this here as well: Pass Parameter with Volley POST

CallMeMSL
  • 1
  • 4
  • By looking at the swagger you need to add three different parameters, are you adding all of these? – Joshua Best Feb 22 '19 at 14:20
  • I need to pick one of the first two parameters, the third one is optional. e. g. this curl command works without problems `curl -X POST "https://www.premiumize.me/api/transfer/create" -H "accept: application/json" -H "Content-Type: multipart/form-data" -F "src=samplemagnetlink"` – CallMeMSL Feb 22 '19 at 14:26
  • Have you made sure the content type is correct? – Joshua Best Feb 22 '19 at 14:37
  • As Pointed out by Tomin, the Content-Type was indeed wrong, however i still get the same response from the api – CallMeMSL Feb 22 '19 at 14:41

3 Answers3

0

Most probably the error is at the header parameter "Content-Type".

You used incorrect header information "multipaltform/form-data"

Try adding the "Content-Type" to "multipart/form-data" to the header data.

The submission of MULTIPARTDATA is different from submission normal form. You have to use MultiPartEntity instead of Map.

Try following this link for Multipart Data: How to send a “multipart/form-data” POST in Android with Volley

Hope this will solve your problem.

Tomin B Azhakathu
  • 2,656
  • 1
  • 19
  • 28
0

Workaround:

Use this Library and this code:

https://github.com/amitshekhariitbhu/Fast-Android-Networking

void pushMagnet(final String apiKey, final String magnetLink, final Context context) {
    final String url = "https://premiumize.me/api/transfer/create?apikey=" + apiKey;
    JSONObject srcMagnet = new JSONObject();
    try {
        srcMagnet.put("src", magnetLink);
    } catch (JSONException e) {
        e.printStackTrace();
    }

    OkHttpClient client = new OkHttpClient().newBuilder()
            .addInterceptor(new GzipRequestInterceptor()).build();
    AndroidNetworking.initialize(context, client);

    AndroidNetworking.post(url)
            .addHeaders("accept", "application/json")
            .addHeaders("Content-Type", "multipart/form-data")
            .addQueryParameter("src", magnetLink)
            .build().getAsJSONObject(new JSONObjectRequestListener() {
        @Override
        public void onResponse(JSONObject response) {
            Log.e("Server response:", response.toString());
        }

        @Override
        public void onError(ANError anError) {
            Log.e("Server error:", anError.toString());
        }
    });

}
CallMeMSL
  • 1
  • 4
0

For sending parameters in volley you need to override the getParams()

@Override
protected Map<String,String> getParams()
{

    Map<String,String> params = new HashMap<>();
    // the POST parameters:
    params.put("src", magnetLink);
    Log.d("Volley","Volley params "+params);
    return params;

}