nst / STHTTPRequest

Obj-C / Cocoa HTTP requests for humans
BSD 3-Clause "New" or "Revised" License
826 stars 75 forks source link

Create a soap request using STHTTPRequest2 #35

Closed paresh-navadiya closed 9 years ago

paresh-navadiya commented 9 years ago

I have native request like this :

 NSString *soapMessage = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
                         "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
                         "<soap:Body>"
                         "<CelsiusToFahrenheit xmlns=\"http://www.w3schools.com/webservices/\">"
                         "<Celsius>140.0</Celsius>"
                         "</CelsiusToFahrenheit>"
                         "</soap:Body>"
                         "</soap:Envelope>"];

NSString *msgLength = [NSString stringWithFormat:@"%lu", (unsigned long)[soapMessage length]];
NSData *soapData = [soapMessage dataUsingEncoding:NSUTF8StringEncoding];

NSURL *url = [NSURL URLWithString:@"http://w3schools.com/webservices/tempconvert.asmx"];

NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
[theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue: @"http://www.w3schools.com/webservices/CelsiusToFahrenheit" forHTTPHeaderField:@"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody:soapData];

Now request using STHTTPRequest2 that i tried and was not successfull.

 STHTTPRequest  * request = [STHTTPRequest requestWithURLString:@"http://w3schools.com/webservices/tempconvert.asmx"];

request.HTTPMethod = @"POST";
[request setHeaderWithName:@"text/xml; charset=utf-8" value:@"Content-Type"];
[request setHeaderWithName:msgLength value:@"Content-Length"];
[request setHeaderWithName:@"http://www.w3schools.com/webservices/CelsiusToFahrenheit" value:@"SOAPAction"];

request.rawPOSTData = soapData;

request.completionBlock = ^(NSDictionary *headers, NSString *body)
{
    NSLog(@"headers = %@\nbody = %@",headers,body);
};

request.errorBlock = ^(NSError *error)
{
     NSLog(@"%@",[error description]);
};

[request startAsynchronous];
nst commented 9 years ago

Does it work with STHTTPRequest 1?

paresh-navadiya commented 9 years ago

No its not working in STHTTPRequest 1 as its throwing same error

Error Domain=STHTTPRequest Code=400 "HTTP Status 400: Bad Request" UserInfo={NSLocalizedDescription=HTTP Status 400: Bad Request}
paresh-navadiya commented 9 years ago

Solved by making necessary changes like say by adding a new method which accepts NSURLRequest :

 + (instancetype)requestWithURLRequest:(NSURLRequest *)urlRequest;

Method defination and also supplementary method added for requirement:

+ (instancetype)requestWithURLRequest:(NSURLRequest *)urlRequest {
   if(urlRequest == nil) return nil;
   return [(STHTTPRequest *)[self alloc] initWithURLRequest:urlRequest];
}

- (instancetype)initWithURLRequest:(NSURLRequest *)theURLRequest {

   if (self = [super init]) {
    self.request = theURLRequest;
    self.url = theURLRequest.URL;
    self.responseData = [[NSMutableData alloc] init];
    self.requestHeaders = [NSMutableDictionary dictionary];
    self.POSTDataEncoding = NSUTF8StringEncoding;
    self.encodePOSTDictionary = YES;
    self.encodeGETDictionary = YES;
    self.addCredentialsToURL = NO;
    self.timeoutSeconds = kSTHTTPRequestDefaultTimeout;
    self.filesToUpload = [NSMutableArray array];
    self.dataToUpload = [NSMutableArray array];
    self.HTTPMethod = theURLRequest.HTTPMethod; // default : GET
    self.cookieStoragePolicyForInstance = STHTTPRequestCookiesStorageUndefined; // globalCookiesStoragePolicy will be used
    self.ephemeralRequestCookies = [NSMutableArray array];
  }
  return self;
}

Changes in startAsynchronous method :

- (void)startAsynchronous {
  NSAssert((self.completionBlock || self.completionDataBlock), @"a completion block is mandatory");
  NSAssert(self.errorBlock, @"the error block is mandatory");

  if (!self.request)
    self.request = [self prepareURLRequest];

 BOOL useUploadTaskInBackground = [self.request.HTTPMethod isEqualToString:@"POST"] && self.request.HTTPBody != nil;

   ------ After above changes make changes for request instance --------
   ------------------------------------------
   -------------------------------------------

}
nst commented 9 years ago

The correct format is:

STHTTPRequest *request = [STHTTPRequest requestWithURLString:@"http://w3schools.com/webservices/tempconvert.asmx"];
[request setHeaderWithName:@"SOAPAction" value:@"http://www.w3schools.com/webservices/CelsiusToFahrenheit"];
request.rawPOSTData = soapData;

request.completionBlock = ^(NSDictionary *headers, NSString *body) {
    NSLog(@"headers = %@\nbody = %@", headers, body);
};

request.errorBlock = ^(NSError *error) {
    NSLog(@"%@",[error description]);
};

[request startAsynchronous];