Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set content-type when using initWithContentsOfUrl

Basically I want to set

Content-Type: application/json;

in order to call a dot net web service and have it return json to an iphone application.

At the moment I have

NSString * jsonres = [[NSString alloc] initWithContentsOfURL:url];

What do I need instead in order to issue a blocking request ?

like image 895
Andiih Avatar asked Aug 24 '09 18:08

Andiih


1 Answers

You need to use NSURLMutableRequest, here you can add headers for content-type and the sort, here is a r eference, http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSMutableURLRequest_Class/Reference/Reference.html#//apple_ref/occ/cl/NSMutableURLRequest, once you set up your request you can add the value using this method - (void)addValue:(NSString *)value forHTTPHeaderField:(NSString *)field something like

[request setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
like image 159
Daniel Avatar answered Sep 25 '22 21:09

Daniel