Note that there are some explanatory texts on larger screens.

plurals
  1. POASIHTTPRequest with xml and image for multipart form on ios
    primarykey
    data
    text
    <p>I need to post to a web server which is expecting xml data as well as an image. The Post request is a mutlipart form. After some research it seemed like most people were recommending <code>ASIHTTPRequest</code>. So I have added this to my project but am still unable to communicate with the server. I get a <code>401 error</code> which I think is returned when the server doesn't get whats expected. (I know that the error handling is not the best but that's all I have to go on). So I have created my <code>ASIHTTPRequest</code> object and used the <code>appendPostData</code> and <code>appendPostDataFromFile</code> methods to attempt to post both the xml and image but had no luck.</p> <p>Here is some of the code :</p> <pre><code> NSMutableData *temp = [[NSMutableData alloc]init]; [temp appendData: [[NSString stringWithFormat: @"&lt;MobileRequest&gt;"] dataUsingEncoding: NSUTF8StringEncoding]]; [temp appendData: [[NSString stringWithFormat: @"&lt;userTokenId&gt;%@&lt;/userTokenId&gt;", @"myUserToken"] dataUsingEncoding: NSUTF8StringEncoding]]; [temp appendData: [[NSString stringWithFormat: @"&lt;applicationTokenId&gt;%@&lt;/applicationTokenId&gt;", @"myAppId"] dataUsingEncoding: NSUTF8StringEncoding]]; [temp appendData: [[NSString stringWithFormat: @"&lt;item&gt;%@&lt;/item&gt;", @"some item string"] dataUsingEncoding: NSUTF8StringEncoding]]; [temp appendData: [[NSString stringWithFormat: @"&lt;attachmentFileName&gt;%@&lt;/attachmentFileName&gt;", @"image.png"] dataUsingEncoding: NSUTF8StringEncoding]]; [temp appendData: [[NSString stringWithFormat: @"&lt;/MobileRequest&gt;"] dataUsingEncoding: NSUTF8StringEncoding]]; NSOperationQueue *queue = [(AppDelegate *)[[UIApplication sharedApplication]delegate]operationQueue]; NSURL *url = [NSURL URLWithString:@"http://myUrl/Request/"]; ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; [request addRequestHeader:@"Content-Type" value:@"text/xml"]; [request appendPostData:temp]; [request appendPostDataFromFile:imageUrlString]; [request setPostBody:temp]; [request setDelegate:self]; [request setDidFinishSelector:@selector(requestDone:)]; [request setDidFailSelector:@selector(requestWentWrong:)]; [queue addOperation:request]; </code></pre> <p>I get a callback in <code>requestWentWrong</code> and the error is the <code>401 Authorization required</code>... which again I think isn't the real problem.</p> <p>Can anyone point in a helpful direction or show me what I'm doing wrong?</p> <p>Any thoughts would be really appreciated.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload