Update User with files to upload getSignedRequestUrl
  • Hi karim,

    I am trying to update a user and upload a file to it HTTP Request.

    When I was using the android sdk I used to this:
    response = client.callAPIJson("SaveUser", parameters, filesMap, AuthMode.SIMPLE);

    and it was working and updating the image field of the user(image of type file in the schema), 

    My question is :
    In HTTP REST request how you send the fileMaps to the request so that it uploads it?

    I tried this :
    response = client.getSignedRequestUrl("SaveUser", parameters, filesMap, AuthMode.SIMPLE);

    and when I run the response as http request , nothing is uploaded..all fields are updated, but files are not uploaded.

    I need to know how you send Files in an HTTP request .

    Thank you.

  • Hi Nour,

    Note that getSignedRequestUrl() does not return the complete URL: it lacks all the parameters that you need to send, so you will have to add them manually to the URL in case of a GET. 

    Since you need to upload a document, you probably need to issue a POST, so you can directly use the result of getSignedRequestUrl() as the value of the "action" attribute of an HTML form, for example, and add all your parameters as inputs fields of the form. Do not forget to add the "enctype" attribute to the latter ("enctype="multipart/form-data") or your file will not be uploaded.

    In cURL, your request should be similar to:
    curl -X POST "https://sandbox.apstrata.com/apsdb/rest/YOUR_AUTH_KEY/SaveUser?apsws.time=1418992125166&apsws.authSig=1933c4b633b2ad05c40c2bd7c7ad0aa8"
    -F apsws.responseType=json -F your_file_field=@image_file.png -F apsdb.login=some_login
    -F apsdb.update=true

    Does this answer your question?
    Karim
  • Hi Nour,

    Your request does not look as a POST request but rather like a GET. Is it the case? Indeed, you should not pass the path to your file in the query string.

    Karim

  • Hi Nour,

    Please try the following code, both versions are working for me (don't specify any content-type):

    // version 1
    UserConnection connection = null;
    try {
    connection = new UserConnection(BASE_URL, ACCOUNT_KEY, USER_NAME, PASSWORD);
    Client client = new Client(BASE_URL, ACCOUNT_KEY, connection);
    List<NameValuePair> parameters = new ArrayList<NameValuePair>();
    BasicNameValuePair login = new BasicNameValuePair("login", "USER_NAME");
    BasicNameValuePair update = new BasicNameValuePair("apsdb.update", "true");
    BasicNameValuePair responseType = new BasicNameValuePair("apsws.responseType", "json");
    parameters.add(login);
    parameters.add(update);
    parameters.add(responseType);
    HashMap<String, List<File>> filesMap = new HashMap<String, List<File>>();
    ArrayList<File> files = new ArrayList<File>();
    File f = new File("C:\\Temp\\file1.png");
    files.add(f);
    filesMap.put("picture", files);
    String response = client.getSignedRequestUrl("SaveUser", parameters, filesMap, AuthMode.SIMPLE);
    response += "&login=" + USER_NAME + "&apsdb.update=true&apsws.responseType=json";
    HttpResponse response2 = null;
    HttpPost httpPost = new HttpPost(response);
    httpPost.setHeader("enctype", "multipart/form-data");
    MultipartEntity multipartEntity = new MultipartEntity();
    httpPost.setEntity(multipartEntity);
    ContentBody contentBody = new FileBody(f);
    multipartEntity.addPart("picture", contentBody);
    HttpClient httpClient = new DefaultHttpClient();
    response2 = httpClient.execute(httpPost);
    HttpEntity resEntity = response2.getEntity();
    InputStream responseStream = response2.getEntity().getContent();
    ...
    } catch (Exception e) {
    e.printStackTrace();
    fail();
    }
    }

    // version 2
    UserConnection connection = null;
    try {
    connection = new UserConnection(BASE_URL, ACCOUNT_KEY, USER_NAME, PASSWORD);
    Client client = new Client(BASE_URL, ACCOUNT_KEY, connection);
    List<NameValuePair> parameters = new ArrayList<NameValuePair>();
    BasicNameValuePair login = new BasicNameValuePair("login", "USER_NAME");
    BasicNameValuePair update = new BasicNameValuePair("apsdb.update", "true");
    BasicNameValuePair responseType = new BasicNameValuePair("apsws.responseType", "json");
    parameters.add(login);
    parameters.add(update);
    parameters.add(responseType);
    HashMap<String, List<File>> filesMap = new HashMap<String, List<File>>();
    ArrayList<File> files = new ArrayList<File>();
    File f = new File("C:\\Temp\\file1.png");
    files.add(f);
    filesMap.put("picture", files);
    String response = client.getSignedRequestUrl("SaveUser", parameters, filesMap, AuthMode.SIMPLE);
    HttpResponse response2 = null;
    HttpPost httpPost = new HttpPost(response);
    httpPost.setHeader("enctype", "multipart/form-data");
    MultipartEntity multipartEntity = new MultipartEntity();
    httpPost.setEntity(multipartEntity);
    multipartEntity.addPart("login", new StringBody(USER_NAME, Charset.forName("UTF-8")));
    multipartEntity.addPart("apsdb.update", new StringBody("true", Charset.forName("UTF-8")));
    multipartEntity.addPart("apsws.responseType", new StringBody("json", Charset.forName("UTF-8")));
    ContentBody contentBody = new FileBody(f); // yourFile is an instance of File
    multipartEntity.addPart("picture", contentBody);
    HttpClient httpClient = new DefaultHttpClient();
    response2 = httpClient.execute(httpPost);
    HttpEntity resEntity = response2.getEntity();
    InputStream responseStream = response2.getEntity().getContent();
    ...
    } catch (Exception e) {
    e.printStackTrace();
    fail();
    }
    }

    Keep me posted
    Karim
  • Hi Karim,

    Concerning uploading a file using REST API

    I implementing First option in your previous answer here in the thread, The image is being updated in the User schema but when I try parsing the response to check the status if successful

    I got the below

    for 
    InputStream responseStream = response2.getEntity().getContent();









    org.apache.http.conn.EofSensorInputStream@4307e060


    for

    response2 = httpClient.execute(httpPost);











    org.apache.http.message.BasicHttpResponse@430672b8and if I analyse the stream
    // Read it all into a String
    BufferedReader br = new BufferedReader(new InputStreamReader(is));
    String line, result = "";
    String eol = System.getProperty("line.separator");
    while ((line = br.readLine()) != null)
    result += line + eol;

    //Log.v("Result After process before the catch", "" + result);
    // Process the JSON response
    JSONObject object = null;
    try {
    Log.v("Hello", "Hello");
    object = (JSONObject) new JSONTokener(result).nextValue();
    Log.v(TAG, "JSONObject object" + object);
    } catch (JSONException e) {
    Log.v(TAG, "Server response:" + result);
    Log.e(TAG, "Server did not return valid JSON", e);
    object = null;
    } catch (ClassCastException e) {
    Log.v(TAG, "Server response:" + result);
    Log.e(TAG, "Server did not return valid JSON", e);
    object = null;
    }






    // And let the process continue
    return object;


    I get this error 











    {"response":{"metadata":{"statusCode":"400","errorCode":"FIELD_FILE_NOT_DEFINED","errorDetail":"When a document is tied to a schema, files can only be saved to fields of type file defined in this schema.","status":"failure","requestId":"d2f29470-a164-43ec-8d3e-28e369830531"}}}

    If I run the link on a browser I get this
    {"response": {
    "metadata": {
    "requestId": "5f63d7c7-7a1f-4fce-94f2-ab29974fd738",
    "status": "success",
    "statusCode": "200"
    }
    }}

    Could you please advise how to get the response metadata and status when uploading or updating a user REST API

    Thank you.
  • hi Karim,

    was my fault I had to replace
    multipartEntity.addPart("picture", contentBody);
    with 
    multipartEntity.addPart("image", contentBody);


    Thank you.
  • Glad you found the problem.

    Karim

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!