Android™
Android™
Capture the Composed REST Request for Debugging
See more REST Examples
Demonstrates Rest.GetLastDebugRequest, which copies the fully composed HTTP request (request line, headers, and body) into a BinData. The example builds a two-part multipart request so the captured output shows the multipart boundaries, part headers, and part bodies. This works when the DebugMode property was enabled while the request was composed.
Background. When DebugMode is enabled, Chilkat builds the request but does not transmit it, so GetLastDebugRequest can reveal the exact bytes that would be sent. Inspecting the composed request is valuable for diagnosing API problems.
Chilkat Android™ Downloads
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;
import android.app.Activity;
import com.chilkatsoft.*;
import android.widget.TextView;
import android.os.Bundle;
public class SimpleActivity extends Activity {
private static final String TAG = "Chilkat";
// Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
boolean success = false;
CkRest rest = new CkRest();
boolean bTls = true;
boolean bAutoReconnect = true;
success = rest.Connect("example.com",443,bTls,bAutoReconnect);
if (success == false) {
Log.i(TAG, rest.lastErrorText());
return;
}
// Enable debug mode so the fully composed HTTP request is captured instead of being sent.
rest.put_DebugMode(true);
// Build a multipart request with two parts. The captured debug output will show the multipart
// boundaries, part headers, and part bodies.
rest.put_PartSelector("1");
rest.AddHeader("Content-Type","text/plain");
rest.AddHeader("Content-Disposition","form-data; name=\"field1\"");
success = rest.SetMultipartBodyString("value1");
if (success == false) {
Log.i(TAG, rest.lastErrorText());
return;
}
rest.put_PartSelector("2");
rest.AddHeader("Content-Type","application/json");
rest.AddHeader("Content-Disposition","form-data; name=\"metadata\"");
success = rest.SetMultipartBodyString("{ \"active\": true }");
if (success == false) {
Log.i(TAG, rest.lastErrorText());
return;
}
// Compose the multipart request. In debug mode the request is built but not transmitted.
success = rest.SendReqMultipart("POST","/api/upload");
if (success == false) {
Log.i(TAG, rest.lastErrorText());
return;
}
// Copy the composed request (request line, headers, and multipart body that would be sent) into a
// BinData, then view it as text.
CkBinData bdRequest = new CkBinData();
rest.GetLastDebugRequest(bdRequest);
String requestText = bdRequest.getString("utf-8");
if (bdRequest.get_LastMethodSuccess() == false) {
Log.i(TAG, bdRequest.lastErrorText());
return;
}
Log.i(TAG, requestText);
}
static {
System.loadLibrary("chilkat");
// Note: If the incorrect library name is passed to System.loadLibrary,
// then you will see the following error message at application startup:
//"The application <your-application-name> has stopped unexpectedly. Please try again."
}
}