2

Im having problems with my request to a asp .net mvc web service. I read in a thread a while ago that its possible to find out what the server wants the the request's content-type to be etc. I get no error when compiling but when I do the actual request nothing happens and in the log of the server it only says (null) (null). There is no problem doing the GET request and fethcing all objects that are in the list. Can anyone please help me with this irritating bug? here is the code:

//----------------GET request to webservice works fine----------------------------------------
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: url];
[request setHTTPMethod: @"GET"];
NSData *response = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil];
NSString *stringResponse = [[NSString alloc] initWithData: response encoding: NSUTF8StringEncoding];
//NSLog(@"stringResponse is %@", stringResponse);
//--------------------------------------------------------------------------------------------
NSString *twitterTrendsUrl=@"http://search.twitter.com/trends.json";
NSString *output=
[NSString stringWithContentsOfURL:[NSURL URLWithString:twitterTrendsUrl]];
id theObject=   [output JSONValue];
NSLog(@"TWITTER: %@",theObject);
*/
//--------------------------------------------------------------------------------------------

NSURL *url = [NSURL URLWithString:@"http://errorreport.abou.se/Errors/1.0"];



//NSString *jsonRequest = @"{\"Description\":\"Gurras Description\",\"Category\":\"Klotter\"}";
//NSString *jsonRequest = @"{\"Description\":\"Gurras Description\",\"Category\":\"Klotter\",\"Address\":\"smedjegatan\",\"StreetNumber\":\"34\",\"Feedback\":\"True\",\"FeedbackWay\":\"Telefon\"}";

NSMutableDictionary* jsonObject = [NSMutableDictionary dictionary];
//NSMutableDictionary* metadata = [NSMutableDictionary dictionary];
//[metadata setObject:@"NewLoc" forKey:@"Uri"];
//[metadata setObject:@"Location.NewLoc" forKey:@"Type"];
//[jsonObject setObject:metadata forKey:@"__metadata"];
[jsonObject setObject:@"Gurras" forKey:@"Description"];
[jsonObject setObject:@"Klotter" forKey:@"Category"];
[jsonObject setObject:@"smedjegatan" forKey:@"Address"];
[jsonObject setObject:@"34" forKey:@"StreetNumber"];
[jsonObject setObject:@"True" forKey:@"Feedback"];
[jsonObject setObject:@"Telefon" forKey:@"FeedbackWay"];
// ... complete the other values
// 
NSString* jsonRequest = [jsonObject JSONRepresentation];
// jsonString now contains your example strings.

NSLog(@"Request: %@", jsonRequest);

//NSURL *url = [NSURL URLWithString:@"https://mydomain.com/Method/"];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
NSData *requestData = [NSData dataWithBytes:[jsonRequest UTF8String] length:[jsonRequest length]];

[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"json" forHTTPHeaderField:@"Data-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody: requestData];

//[NSURLConnection connectionWithRequest:[request autorelease] delegate:self];

NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil ];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding: NSUTF8StringEncoding];

NSLog(@"returnData: %@", returnString);

I can also add an example of how to talk to the service with javascript:

<script type="text/javascript">
var obj = { "Description": "det kanske funkar" };
$(document).ready(function () {
    $.ajax({
        type: "POST",
        url: "/Errors/1.0",
        dataType: "json",
        contentType: "application/json",
        processData: true,
        data: '{"Description": "STeffeent   asdasd", "Category": "Miljö", "Address": "Bogatan","StreetNumber": "14", "Feedback": "true", "FeedbackWay": "Brev"}',
        success: function (data) {
            $("#result").text(data.Description);
        }
    });
});

Gustav Engvall
  • 135
  • 1
  • 2
  • 8
  • Might be worth checking all the parameters for your request first, using another tool. SoapUI is good (http://www.soapui.org/) – Colin Pickard Apr 11 '11 at 13:27
  • Is it similar to RESTClient you think? Becuse I have tried it there and no problem comunicating with the server. – Gustav Engvall Apr 11 '11 at 13:35
  • since it's not a Soap message does soapui work at all? – Gustav Engvall Apr 12 '11 at 06:21
  • I've not use RESTClient but I use SoapUI a lot when debugging REST services like this. It works really well with JSON requests. The documentation has a getting started guide: http://www.soapui.org/REST-Testing/getting-started.html. It's always best to separate debugging the server from debugging your own code. Sometimes I have found that even though the server seems to working fine, one tiny change in the request parameters means you get no response at all or just an error. It really depends on the server. – Colin Pickard Apr 12 '11 at 09:00
  • tnx will look into soapui then. – Gustav Engvall Apr 12 '11 at 09:48
  • 2
    Solved the problem guys! Tried to paste the solution in an answer but can't doit until tomorrow. Thanks to soapUI and Colin I got the request parameters right. It turned out to be some errors in the JSON object to. Some parameters expected to be strings and I sent integers and so on. – Gustav Engvall Apr 12 '11 at 12:53

0 Answers0