简体   繁体   中英

HTTP PUT or DELETE request with JSONHTTPClient in iOS and OS-X

I have just started working with JSONModel . JSONHTTPClient can handle asynchronous network request supporting "GET" and "POST" methods. Is there a way out to make a "PUT" or "DELETE" request using JSONHTTPClient?

Looking at the documentation here

http://jsonmodel.com/docs/Classes/JSONHTTPClient.html#//api/name/requestHeaders

It doesn't seem you can do it. You will have to use another approach to do this. May be another framework or NSURLSessions or write your own category over the JSONHTTPClient class.

Anyway I came with the most stupid solution by adding new methods to JSONHTTPClient.

+(void)putJSONFromURLWithString:(NSString)urlString params:(NSDictionary)params completion:(JSONObjectBlock)completeBlock;
+(void)putJSONFromURLWithString:(NSString)urlString bodyString:(NSString)bodyString completion:(JSONObjectBlock)completeBlock;
+(void)putJSONFromURLWithString:(NSString)urlString bodyData:(NSData)bodyData completion:(JSONObjectBlock)completeBlock;

I also added a new const NSString to support "PUT"

NSString* const kHTTPMethodPUT = @"PUT";

+(void)putJSONFromURLWithString:(NSString*)urlString params:(NSDictionary*)params completion:(JSONObjectBlock)completeBlock
{
    [self JSONFromURLWithString:urlString method:kHTTPMethodPUT
                         params:params
                   orBodyString:nil completion:^(id json, JSONModelError* e) {
                       if (completeBlock) completeBlock(json, e);
                   }];

}

+(void)putJSONFromURLWithString:(NSString*)urlString bodyString:(NSString*)bodyString completion:(JSONObjectBlock)completeBlock
{
    [self JSONFromURLWithString:urlString method:kHTTPMethodPUT
                         params:nil
                   orBodyString:bodyString completion:^(id json, JSONModelError* e) {
                       if (completeBlock) completeBlock(json, e);
                   }];
}


+(void)putJSONFromURLWithString:(NSString*)urlString bodyData:(NSData*)bodyData completion:(JSONObjectBlock)completeBlock
{
    [self JSONFromURLWithString:urlString method:kHTTPMethodPUT
                         params:nil
                   orBodyString:[[NSString alloc] initWithData:bodyData encoding:defaultTextEncoding]
                     completion:^(id json, JSONModelError* e) {
                         if (completeBlock) completeBlock(json, e);
                     }];
}

I also created an issue at GitHub . Hope to get response from there.

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