0

I have a REDCap project complete setup on Redcap console .

API token generated .

Record saving working from REDCap .

Also from browser tool.

But when I call it from Android App it returns 403 forbidden .

is there anything like set permission for a user.

Also same is working perfectly from ios app .

 HashMap<String, String> params = new HashMap<String, String>();
       
        params.put("token","MY TOKEN");
        params.put("content","record");

OkHttpClient client = new OkHttpClient();

        RequestBody body = RequestBody.create(JSON, String.valueOf(params));
        Request request = new Request.Builder()
                .url("MY_URL")
                .post(body)
                .addHeader("Content-Type", "application/x-www-form-urlencoded")
                .build();

        client.newCall(request).enqueue(new Callback() {


            @Override
            public void onFailure(com.squareup.okhttp.Request request, IOException e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(com.squareup.okhttp.Response response) throws IOException {
                if (!response.isSuccessful()) {
                    throw new IOException("Unexpected code " + response);
                } else {
                    // do something wih the result
                    Log.d("check ok http response ", response.toString());
                }
            }

    });

From Browser tool if I put same URL with selecting POST and set only two params token and content , it return 200 OK .

But from Android it returns 403 . Please help , I have tried several methods in android code .

wibeasley
  • 5,000
  • 3
  • 34
  • 62

1 Answers1

0

You're doing this:

RequestBody body = RequestBody.create(JSON, String.valueOf(params));

that's not a valid form body. Do this:

FormBody.Builder formBuilder = new FormBody.Builder()
    .add("token","MY TOKEN").add("content","record");

and then

Request request = new Request.Builder()
            .url("MY_URL")
            .post(formBuilder.build())
            .addHeader("Content-Type", "application/x-www-form-urlencoded")
            .build();
Femi
  • 64,273
  • 8
  • 118
  • 148