简体   繁体   中英

How to set JSON NSData with ASIFormDataReuest

Hi I want to pass my userName and Password to WCF Webservice from iPhone. I'm using ASIFormDataRequest.

Here is my code

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    [request setRequestMethod:@"POST"];
    [request setPostValue:txtUsername forKey:@"userName"];
    [request setPostValue:txtPassword forKey:@"password"];
    [request setDelegate:self];
    [request addRequestHeader:@"Content-Type" value:@"application/json; charset=utf-8"]; 
    [request startAsynchronous];

How do I pass JSON Data? I could use this... [request appendPostBody:jsonData]; // JSON as NSData

but how do I set JSON to NSData?

NSData *jsonData = @"{'userName':'john', 'password':'secret'}";

Please help me!

Supposing you want to encode the JSON in UTF-8 as stated in your header:

NSString *json = @"{'userName':'john', 'password':'secret'}";
NSData *jsonData = [json dataUsingEncoding:NSUTF8StringEncoding];
[request appendPostBody:jsonData];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM