0

I have a following image upload api which works fine.

CURL --user username:password -X POST -H 'Content-Disposition: filename=scarlett-judinna.jpg' --data-binary @'/Volumes/Work/tmp/Dummy Images/image.jpg' http://example.com/wp-json/wp/v2/media

Now i want to upload image from android using this api.

I have tried the following process.

params.put("image", new ByteArrayInputStream(byte_arr));
AsyncHttpClient client = new AsyncHttpClient();
client.setBasicAuth(username, password);
client.addHeader("Content-Disposition",  "filename=" + fileName);
client.addHeader("Content-Type", "image/" + extension);
client.post("http://example.com/wp-json/wp/v2/media",
            params, new AsyncHttpResponseHandler() {
     @Override
     public void onSuccess(String response) {
          prgDialog.hide();
          Toast.makeText(getApplicationContext(), response,
              Toast.LENGTH_LONG).show();
    }

    @Override
    public void onFailure(int statusCode, Throwable error,
            String content) {
       Toast.makeText(getApplicationContext(), statusCode,Toast.LENGTH_LONG).show();
    }
});

This always goes to the onFailure method with statusCode 403

So what am i doing wrong?

Stephane Landelle
  • 6,990
  • 2
  • 23
  • 29
Sangharsha
  • 88
  • 11

1 Answers1

0

status code 403 suggests that the request is forbidden. Perhaps the folder you are uploading to the server doesn't have write permissions.

Julian Raj
  • 62
  • 2
  • But when i m using same api in web to upload image... and there is no such problem. – Sangharsha Jan 20 '16 at 08:54
  • is the api protected with some oAuth? could you share the response body and message ... – Julian Raj Jan 21 '16 at 04:47
  • yes it has basic auth. but i have defined it... `client.setBasicAuth(username, password);` the response is: [{"data":{"status":403},"message":"You don't have permission to do this.","code":"rest_forbidden"}] – Sangharsha Jan 24 '16 at 09:22