I am using NSMutableURLRequest to upload parameters to server using SOAP API and it is working fine.
static NSString *URLString = #"http://www.myURL";
NSMutableURLRequest *request =
[NSMutableURLRequest requestWithURL:[NSURL URLString]];
[request addValue:#"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[request setHTTPMethod:#"POST"];
[request addValue:#"http://..." forHTTPHeaderField:#"SOAPAction"];
NSString *name = #"John";
NSString *parameters = [NSString stringWithFormat:
#"<s:Envelope xmlns:s=\"http://..../\">\n"
"<s:Body >\n"
"<name>%#</name>\n"
"</s:Body>\n"
"</s:Envelope>\n", name];
[request setHTTPBody:[parameters dataUsingEncoding:NSUTF8StringEncoding]];
_myConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
Now I want to send a text file (attachment) along with the parameters.
Can someone help?
Hi you can convert your file into NSDATA then into BASE64 String and add as parameter to your request body and after that at backend you can convert BASE64 String to Binary Data and save it as text file.
Encoding
NSString *str=[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"file.txt"];
NSData *data = [NSData dataWithContentsOfFile:str];
NSString *base64String = [data base64EncodedStringWithOptions:0];
NSLog(#"%#", base64String);
Here is the link for BASE64 Conversion classes
Just add one more parameter
NSString *parameters = [NSString stringWithFormat:
#"<s:Envelope xmlns:s=\"http://..../\">\n"
"<s:Body >\n"
"<name>%#</name>\n"
"<fileData>%#</fileData>\n" //Here is new parameter
"</GetChanges>\n"
"</s:Body>\n"
"</s:Envelope>\n", name, base64String];
Related
NSString *phone = [NSString stringWithFormat:#"phone=%#",[paymentServerData valueForKey:#"phone"]];
NSString *key = [NSString stringWithFormat:#"key=%#",[paymentServerData valueForKey:#"key"]];
NSString *furl = [NSString stringWithFormat:#"furl=%#",[paymentServerData valueForKey:#"furl"]];
NSString *txnid = [NSString stringWithFormat:#"txnid=%#",[paymentServerData valueForKey:#"txnid"]];
NSString *productinfo =[NSString stringWithFormat:#"productinfo=%#",[paymentServerData valueForKey:#"txnid"]];
NSArray *arrayString = [[NSArray alloc] initWithObjects phone, key,furl,txnid,productinfoEncode,nil];
NSString *postString = [arrayString componentsJoinedByString:#"&"];
NSString *address = #"https://secure.payu.in/_payment";
NSURL *url = [NSURL URLWithString: address];
NSString *body = [NSString stringWithFormat: #"%#", postString];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL: url];
[request setHTTPMethod: #"POST"];
[request setHTTPBody: body];
[_paymentWebView loadRequest: request];
i have 5 parameters to pass the body string but i want encode only one parameter how to encode one parameter and send the body method.
help me to encode this productionfo string.
// this line for encoding your data .
NSData *postData = [productinfo dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSURL *url = [NSURL URLWithString:#"URL"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
[theRequest setHTTPMethod:#"POST"];
[theRequest setHTTPBody:postData];
I need to pass some data in order for the server to the save the info,in order to do this the parameters need to pass the parameters in XML nodes following a url http://xxx.xxx.xx.xxx/WCF/Service1.svc/XXX/Upload?sXML=
Here is an example of the XML node structure
<Send_info>
<Service_order>
</Service_order>
<Data_stream>
<info>
</info>
</Data_stream>
</Send_info>.
The return value would be a JSON string with {"Uploaded":"TRUE"} or {"Uploaded":"FALSE"} respectively.
I have tried this method with no answer it just return nil.
-(void)Request:(NSData*)data{
NSURL *aUrl = [NSURL URLWithString:#"http://xxx.xxx.xx.xxx/WCF/Service1.svc/XXX/Upload?sXML="];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:aUrl
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
[request setHTTPMethod:#"GET"];
[request setHTTPBody:data];
NSURLConnection *connection= [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection start];
}
edit:you can find the answer in the comments
The answer was quite simple just use these blocks of code and substitute them with your own parameters
POST:
-(void)webservicepost:(NSString *)poststring{
NSString *poststring = #"String in the format we need to upload";
NSURL *remoteURL = [NSURL URLWithString:#"URL in which we'll upload"];
NSMutableURLRequest *Request = [[NSMutableURLRequest alloc] initWithURL:remoteURL];
[Request addValue:#"application/json; charset=utf-8" forHTTPHeaderField: #"Content-Type"];//Formats for the data
NSMutableData *body = [NSMutableData data];
[body appendData:[poststring dataUsingEncoding:NSUTF8StringEncoding]]; //Add content and coding
[Request setHTTPMethod:#"POST"]; //POST method
[Request setHTTPBody:body];
NSData *data = [NSURLConnection sendSynchronousRequest:Request returningResponse:nil error:nil];//Server response in data form
NSString *sdata = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];//Server response in string form
}
GET:
-(void)get:(NSString *)myxmlstring{
NSString *escapedString = [myxmlstring stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *urlString = [NSString stringWithFormat:#"http://xxx.xxx.xx.xxx/WCF/Service1.svc/XXX/Upload?sXML=%#",escapedString];
NSURL *url = [NSURL URLWithString:urlString];
NSData *data = [NSURLConnection sendSynchronousRequest:Request returningResponse:nil error:nil];//Server response in data form
NSString *sdata = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];//Server response in string form
//FROM HERE YOU CAN USE THE RETRIEVED INFORMATION FROM THE URL
}
I'm currently working on sending uiimages to my server to be stored on the server. So I get an image from the user gallery and then encode it using Base64 and then send it to my server. However all the images I sent to my server don't show. I'm wondering whether the post request I make with the base64encodedstring alters the encoded string.
Here is what I have done.
//Upload picture to server
NSData *imageData=UIImageJPEGRepresentation(showPic.image, 1.0f);
NSLog(#"The size of the image is %i",[imageData length]);
[Base64 initialize];
NSString *strEncoded = [Base64 encode:imageData];
//NSLog(#"This is the encoded string to be uploaded %#",encodedPic);
prefs=[NSUserDefaults standardUserDefaults];
userid=[prefs stringForKey:#"userid"];
NSLog(#" %# is the userid to be used to upload the pic",userid);
NSString *poster = #"username=";
poster = [poster stringByAppendingString:userid];
poster = [poster stringByAppendingString:#"&image="];
poster = [poster stringByAppendingString:strEncoded];
NSString *post = poster;
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:#"http://www.example.com/server/upload"]];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
In calling apexrest webservice for uploading attachment to specific record by calling method. So for this I hardcoded Json.
-(void)uploadToSalesforce
{
NSData *imagedata = UIImageJPEGRepresentation(imagePreview.image, 1.0);
int datalength = [imagedata length];
NSString *filename = [NSString stringWithFormat:#"Supload_iPhone_%d.jpg",datalength];
NSString *req = [NSString stringWithFormat:#"{\n\"name\":\"%#\",\n\"Body\": \"%#\"\n,\"ParenId\":%#\"\n}",filename,imagedata,receivedrecordid];
const char *utfString = [req UTF8String];
NSData *postData = [NSData dataWithBytes:utfString length:strlen(utfString)];
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
NSMutableURLRequest *requestUrl = [[NSMutableURLRequest alloc] init ];
[requestUrl setURL:[NSURL URLWithString:[NSString stringWithFormat:#"%#/services/apexrest/Account/",receivedinstanceurl]]];
[requestUrl setHTTPMethod:#"POST"];
[requestUrl setValue:postLength forHTTPHeaderField:#"Content-length"];
[requestUrl setValue:[NSString stringWithFormat:#"Bearer %#",receivedaccesstoken] forHTTPHeaderField:#"Authorization"];
[requestUrl setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
[requestUrl setHTTPBody:postData];
NSURLResponse *response;
NSError *err;
NSData *reponseData = [NSURLConnection sendSynchronousRequest:requestUrl returningResponse:&response error:&err];
NSString *res = [[NSString alloc] initWithData:reponseData encoding:NSASCIIStringEncoding];
}
In response it says there is
[{"message":"Unexpected parameter encountered during deserialization: Name at [line:2, column:9]","errorCode":"JSON_PARSER_ERROR"}]
In console JSON seems correct but cannot parse parameter "Name".I think this is not by IOS code. Or is there some different format.
In the line
NSString *req = [NSString stringWithFormat:#"{\n\"name\":\"%#\",\n\"Body\": \"%#\"\n,\"ParenId\":%#\"\n}",filename,imagedata,receivedrecordid];
JSON is missing " character for ParentId key value. It should be:
NSString *req = [NSString stringWithFormat:#"{\n\"name\":\"%#\",\n\"Body\": \"%#\"\n,\"ParenId\":\"%#\"\n}",filename,imagedata,receivedrecordid];
Therefore Salesforce webservice deserialization was throwing exception.
I've managed to get simple text data sent to a server using this code:
NSMutableString *parameterString = [[NSMutableString alloc] initWithString: #""];
[parameterString appendString:#"name=steve&"];
[parameterString appendString:#"surname=jobs&"];
[parameterString appendString:#"age=55"];
NSURL *url = [NSURL URLWithString:#"http://example.come/script/"];
request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0];
[request setHTTPMethod:#"POST"];
NSData *parameterData = [parameterString dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:parameterData];
With that I can send text form data. But how can I send PNG images as well?
You could just convert the image to an NSData object, then base64 encode it to send it as a parameter.
Base64 encoding examples found here: How do I do base64 encoding on iphone-sdk?
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request addData:imageData withFileName:#"image.png" andContentType:#"image/png" forKey:#"yourParamKey"];