REST - POST Package JSON + Documents Assistance
Sunday, June 12, 2016 at 04:57pmHello,
still in exploratory stages at the moment. does that team has an example to share the illustrates how, using objectivec, one could http POST both a json package and document to sign using the REST api?
i'm trying to achieve the same outcome as the PYTHON script; simply replicate in objectivec.
any assistance would be greatly appreciated!!
the python script makes it look to easy!
# test1 for creating package
# payload = codecs.open('package.json', 'r',encoding='utf8').read()
# print(payload)
# documents = {'file': open(os.getcwd()+'/newaccount-v1.pdf', 'rb').read()}
# print("creating package")
# print(client.create_package_multipart(payload, documents))
# test1 end
cheers.
Reply to: REST - POST Package JSON + Documents Assistance
Monday, July 11, 2016 at 11:20amNSString *payload = @"{\"status\": \"DRAFT\",\"roles\": [{\"id\": \"signer1\",\"signers\": [{\"email\": \"[email protected]\",\"firstName\": \"Qamar\",\"lastName\": \"Suleiman\"}]}],\"documents\": [{\"description\": \"this is description\",\"approvals\": [{\"role\": \"signer1\",\"fields\": [{\"type\":\"SIGNATURE\",\"subtype\": \"FULLNAME\",\"page\": 0,\"top\": 100,\"left\": 100,\"width\": 200,\"height\": 60}]}],\"name\": \"Document1\"}],\"name\": \"TransactionTest\"}"; NSString *filePath = [[NSBundle mainBundle] pathForResource:@"testPDF" ofType:@"pdf"]; NSData *fileData = [NSData dataWithContentsOfFile:filePath]; NSString *serverAPIURL = @"https://sandbox.esignlive.com"; NSURL *url = [NSURL URLWithString:[serverAPIURL stringByAppendingPathComponent:@"api/packages"]]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; /* We Could Either use the Session token or the API Key */ NSString *sessionToken = @"";
sessionToken = nil;
if (sessionToken) {
NSDictionary *cookieProperties = [NSDictionary dictionaryWithObjectsAndKeys:
serverAPIURL, NSHTTPCookieDomain,
@"\\", NSHTTPCookiePath,
@"ESIGNLIVE_SESSION_ID", NSHTTPCookieName,
sessionToken, NSHTTPCookieValue,
nil];
NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:cookieProperties];
NSArray* cookieArray = [NSArray arrayWithObjects: cookie, nil];
NSDictionary * headers = [NSHTTPCookie requestHeaderFieldsWithCookies:cookieArray];
[request setAllHTTPHeaderFields:headers];
}
else
{
NSString *apiKEY = @"";
[request setValue:[NSString stringWithFormat:@"Basic %@",apiKEY] forHTTPHeaderField: @"Authorization"];
}
[request setHTTPMethod:@"POST"];
//setup the body
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"payload\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[payload dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file\"; filename=\"%@\"\r\n",[filePath lastPathComponent]] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:fileData];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue new] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
NSLog(@"%@",responseDict);
}]; Reply to: REST - POST Package JSON + Documents Assistance
Wednesday, June 15, 2016 at 02:52pmReply to: REST - POST Package JSON + Documents Assistance
Wednesday, June 15, 2016 at 03:00pm- (void)submitPackageJSONv3:(NSDictionary*)json document:(NSData*)document { // prepare multipart boundary NSString *boundary = @"unique-consistent-string"; NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary]; // ---- initialize request object ---- NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://sandbox.esignlive.com/api/packages"]]; [request setHTTPMethod:@"POST"]; // ---- set headers ---- [request setValue:@"Basic enVyZ........................4b0U4Tw==" forHTTPHeaderField: @"Authorization"]; [request setValue:@"gzip, deflate" forHTTPHeaderField: @"Accept-Encoding"]; [request setValue:@"*/*" forHTTPHeaderField: @"Accept"]; [request setValue:@"keep-alive" forHTTPHeaderField: @"Connection"]; [request setValue:@"chunked" forHTTPHeaderField: @"Transfer-Encodinge"]; [request setValue:contentType forHTTPHeaderField: @"Content-Type"]; NSMutableData *body = [NSMutableData data]; // ---- apppend JSON ---- [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"%@\r\n", @"Content-Disposition: form-data; name='payload'"] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[NSKeyedArchiver archivedDataWithRootObject:json]]; // ---- apppend docs ---- [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"%@\r\n", @"Content-Disposition: form-data; name='file'; filename='file'"] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:document]; [body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; // ---- set HTTP Body ---- [request setHTTPBody:body]; // ---- set the content-length header ---- NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[body length]]; [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; // ---- send HTTP POST ---- [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { NSLog(@"%@", error.description); if(data.length > 0) { //success NSLog(@"Success"); } }]; }Reply to: REST - POST Package JSON + Documents Assistance
Saturday, June 18, 2016 at 12:29pm2016-06-18 17:11:04.533 ESLRemoteExpert[82361:18662663] {"messageKey":"error.internal.default","technical":"65533","message":"Unexpected Error. Our technical staff have been notified and will look into the problem. We apologize for any inconvenience this may have caused you.","code":500,"name":"Unhandled Server Error"}i suspect this is a file encoding issue. ? any suggestions would be greatly appreciated.- (void)submitPackageJSONv5:(NSDictionary*)json documentPath:(NSString*)documentPath { // prepare multipart boundary NSString *boundary = @"unique-consistent-string"; NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary]; // ---- initialize request object ---- NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://sandbox.esignlive.com/api/packages"]]; [request setHTTPMethod:@"POST"]; // ---- set headers ---- [request setValue:@"Basic enVyZ1A3Yl....................YTV1eW94b0U4Tw==" forHTTPHeaderField: @"Authorization"]; [request setValue:@"gzip, deflate" forHTTPHeaderField: @"Accept-Encoding"]; [request setValue:@"*/*" forHTTPHeaderField: @"Accept"]; [request setValue:@"keep-alive" forHTTPHeaderField: @"Connection"]; [request setValue:@"chunked" forHTTPHeaderField: @"Transfer-Encodinge"]; [request setValue:contentType forHTTPHeaderField: @"Content-Type"]; NSMutableData *body = [NSMutableData data]; // ---- apppend JSON ---- [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"%@\r\n", @"Content-Disposition: form-data; name='payload'"] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[NSKeyedArchiver archivedDataWithRootObject:json]]; NSData *filedata = [NSData dataWithContentsOfURL:[NSURL fileURLWithPath:documentPath]]; NSLog(@"file:%@",filedata); // ---- apppend docs ---- [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"%@\r\n", @"Content-Disposition: form-data; name='file'; filename='file'"] dataUsingEncoding:NSUTF8StringEncoding]]; // [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:filedata]; [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [request setHTTPBody:body]; NSHTTPURLResponse* response =[[NSHTTPURLResponse alloc] init]; NSError* error = [[NSError alloc] init] ; error = [NSError errorWithDomain:DGErrorDomain code:DGFNIDParsingFailedError userInfo:nil]; NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; if (error) { NSLog(@"%@", error.description); } NSString *responseString = [[NSString alloc] initWithBytes:[responseData bytes] length:[responseData length] encoding:NSUTF8StringEncoding]; NSLog(@"%@", responseString); }Reply to: REST - POST Package JSON + Documents Assistance
Tuesday, June 21, 2016 at 05:29am2016-06-18 17:11:04.533 ESLRemoteExpert[82361:18662663] {"messageKey":"error.internal.default","technical":"65533","message":"Unexpected Error. Our technical staff have been notified and will look into the problem. We apologize for any inconvenience this may have caused you.","code":500,"name":"Unhandled Server Error"}potentially viewing the server logs?Reply to: REST - POST Package JSON + Documents Assistance
Tuesday, June 21, 2016 at 05:39amReply to: REST - POST Package JSON + Documents Assistance
Tuesday, June 21, 2016 at 11:02am- (void)getRequest { // 1 NSURL *url = [NSURL URLWithString:@"https://sandbox.esignlive.com/api/packages"]; NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration]; NSURLSession *session = [NSURLSession sessionWithConfiguration:config]; // 2 NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url]; request.HTTPMethod = @"GET"; [request setValue:@"Basic enVyZ1A3Y...................94b0U4Tw==" forHTTPHeaderField: @"Authorization"]; // 3 NSError *error = nil; if (!error) { // 4 NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { NSLog(@"%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]); }]; // 5 [dataTask resume]; } }Reply to: REST - POST Package JSON + Documents Assistance
Tuesday, June 21, 2016 at 11:08amReply to: REST - POST Package JSON + Documents Assistance
Monday, July 11, 2016 at 11:23amReply to: REST - POST Package JSON + Documents Assistance
Monday, July 11, 2016 at 01:14pm