1

I have created a custom POST api for getting login information in MCS. when i check in SOAPUI it works perfectly fine. the parameters passed are

1. header   
Oracle-Mobile-Backend-Id: ********************
2. Authentocation
Username:****************
password: **************

and basic login info username and password as "User1" and "user1" respectively.

Step2:

when i call the API from MAF i am getting an error 400 the post method used is

    public static Response callPost(String restURI, String jsonRequest) {
    String responseJson = "";
    Response response = new Response();

    RestServiceAdapter restServiceAdapter = Model.createRestServiceAdapter();
    restServiceAdapter.clearRequestProperties();
    //restServiceAdapter.setConnectionName("MiddlewareAPI");
   // restServiceAdapter.setConnectionName("");


    restServiceAdapter.setRequestType(RestServiceAdapter.REQUEST_TYPE_POST);
    restServiceAdapter.addRequestProperty("Content-Type", "application/json");
    restServiceAdapter.addRequestProperty("Accept", "application/json; charset=UTF-8");
    restServiceAdapter.addRequestProperty("Oracle-Mobile-Backend-Id", "**********");
    restServiceAdapter.addRequestProperty("Domain", "mcsdem0001");


    restServiceAdapter.addRequestProperty("Username", "******");
    restServiceAdapter.addRequestProperty("Password", "*****");
    //restServiceAdapter.addRequestProperty("Authorization", "Basic "+new String(encodedBytes));
    System.out.println("**** Authorization String ****=>"+new String(encodedBytes));
    System.out.println("**** RestURI ******=>"+restURI);
    System.out.println("**** jsonRequest ******=>"+jsonRequest);

    restServiceAdapter.setRequestURI(restURI);
    restServiceAdapter.setRetryLimit(0);

    try {
        responseJson = restServiceAdapter.send(jsonRequest);
        int responseCode = restServiceAdapter.getResponseStatus();
        response.setResponseCode(responseCode);
        response.setResponseMessage(responseJson);
        response.setHeader(restServiceAdapter.getResponseHeaders());
    } catch (Exception e) {
        int responseCode = restServiceAdapter.getResponseStatus();
        response.setResponseCode(responseCode);
        response.setResponseMessage(responseJson);
    }
    System.out.println("Response:" + responseJson);

    return response;
}

Could anyone please tell me is there any error in the post method??

Arj 1411
  • 1,395
  • 3
  • 14
  • 36

3 Answers3

1

actually this bit

restServiceAdapter.addRequestProperty("Username", "******"); restServiceAdapter.addRequestProperty("Password", "*****");

doesn't work because you attempt to pass username and password as a HTTP header. Instead it should be passed as you were trying here

restServiceAdapter.addRequestProperty("Authorization", "Basic "+new String(encodedBytes));

However, these should not be encoded bytes but a base64 encoded string in the form

Basis (without the < abd >)

Note that user identity domains only need to be provided in multi-tenant environments. In MCS, the user domain is defined through the mobile backend you connect to.

Frank

1

This can be due to the version conflict. Try to use HttpUrlConnection instead of RestServiceAdapter and let me know if it works.

0

Use the MAF MCS Utility library to make it allot easier. The developer guide can be found here: http://download.oracle.com/otn_hosted_doc/maf/mafmcsutility-api-doc-082015.pdf

Example code:

MBEConfiguration mbeConfiguration = 
    new MBEConfiguration(
          <mbe rest connection>,<mobileBackendId>,
          <anonymous key string>,<application key string>, 
          MBEConfiguration.AuthenticationType.BASIC_AUTH);
 mbeConfiguration.setEnableAnalytics(true);
 mbeConfiguration.setLoggingEnabled(false)
 mbeConfiguration.setMobileDeviceId(
         DeviceManagerFactory.getDeviceManager().getName());
 MBE mobileBackend = MBEManager.getManager().
         createOrRenewMobileBackend(<mobile backend Id>, mbeConfiguration);

CustomAPI customApiProxy = mbe.getServiceProxyCustomApi();
MCSRequest request = new MCSRequest(mobileBackend.getMbeConfiguration());

request.setConnectionName(<Rest connection name>);
request.setRequestURI("/moile/custom/mockup/employees");
request.setHttpMethod(MCSRequest.HttpMethod.POST);
request.setPayload("{\"id\":\"1\"\"name\":\"nimphius\",\"firstName\":\"frank\"}");
request.setRetryLimit(0);
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type","application/json");
request.setHttpHeaders(headers);
MCSResponse response = customApiProxy .sendForStringResponse(request);
String jsonResponse = (String) response.getMessage();
User404
  • 2,152
  • 2
  • 27
  • 35
  • Could you please tell me how to use it?? – Arj 1411 Sep 22 '15 at 08:38
  • The developer guide explains how to use. The sample code I provided also comes from that document. – User404 Sep 22 '15 at 08:48
  • Thats Ok.. could you please tell me whether is there any problem with my code? – Arj 1411 Sep 22 '15 at 10:08
  • I have followed the steps, but i got an error:: HTTP Status Code 401 Unauthorized: The request requires user authentication. The response MUST include a WWW-Authenticate header field containing a challenge applicable to the requested resource. The client MAY repeat the request with a suitable Authorization header field. – Arj 1411 Sep 23 '15 at 07:41