0

I want to send a document that I obtained as NSData with DocuSign API to a web that has a service to do it.

The service is

POST /api/v1/rest/groups/[:idgroup]/documents/upload/[:oauthtoken]

and the required parameters are:

  1. file --> The file to upload (Type: inputstream)
  2. fileName --> The name of the new file to upload (Type: string)
  3. length --> The size of the file to upload in bytes (Type: long)

I'm trying to do the request with this example Uploading Image via POST in Objective C but I have a service error that says "InvalidParametersServiceApiException"

My code is:

NSString *filename = @"docusignTest.pdf";
NSMutableURLRequest* request= [[NSMutableURLRequest alloc] init];

[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];

NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];

[request addValue:contentType forHTTPHeaderField: @"Content-Type"];

NSMutableData *postbody = [NSMutableData data];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

[postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"fileName\"; \r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

[postbody appendData:[[NSString stringWithFormat:@"\r\n%@",filename] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

[postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"length\"; \r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

[postbody appendData:[[NSString stringWithFormat:@"\r\n%lu",(unsigned long)oResponseData.length] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

[postbody appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

[postbody appendData:[NSData dataWithData:oResponseData]];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

[request setHTTPBody:postbody];

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

NSString *responseString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
Community
  • 1
  • 1
nsanchez
  • 132
  • 2
  • 12
  • I can't see where you are actually converting the PDF into data and including in the body? – Flexicoder Mar 21 '14 at 09:54
  • The data is `oResponseData` and it's correct because if I save the data as a PDF I can read it correctly. `oResponseData` is the response that I obtain of DocuSign service as shown this example http://iodocs.docusign.com/APIWalkthrough/getEnvelopeDocuments – nsanchez Mar 21 '14 at 09:59

2 Answers2

1

Here's a suggestion: Instead of ignoring the response and the error, ask sendSynchronousRequest to supply them both to you, then examine what you are getting. It's rather mad to send a POST request without any error handling. The data you sent might be wrong, and servers are notorious about wanting you to get it right, but good servers will tell you what is wrong. The server may have problems. The URL may be wrong. The device may not have a connection.

And posting exactly what you received, without any interpretation by you, might help. Reading the documentation for the service you are calling might also help. No two services are the same.

gnasher729
  • 51,477
  • 5
  • 75
  • 98
  • Documentation about service is only the information about required parameters that I post in the question. Te data is correct and the URL too because if I do this request in vb.net it's all ok but I'm starting with objective C and I don't know how do this. – nsanchez Mar 21 '14 at 10:03
0

The example of this link is very helpul.

NSString *filename = @"docusignTest.pdf";

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];

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=\"file\"; filename=\"%@\"\r\n", filename] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[NSData dataWithData:oResponseData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"fileName\"\r\n\r\n%@", filename] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"length\"\r\n\r\n%d", oResponseData.length] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

[request setHTTPBody:body];

NSURLResponse *response;
NSError *error2;

NSData *oResponseData2 = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error2];

NSMutableString *jsonResponse2 = [[NSMutableString alloc] initWithData:oResponseData2 encoding:NSUTF8StringEncoding];
nsanchez
  • 132
  • 2
  • 12