简体   繁体   English

在iOS和OS-X中使用JSONHTTPClient进行HTTP PUT或DELETE请求

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

I have just started working with JSONModel . 我刚刚开始使用JSONModel JSONHTTPClient can handle asynchronous network request supporting "GET" and "POST" methods. JSONHTTPClient可以处理支持“ GET”和“ POST”方法的异步网络请求。 Is there a way out to make a "PUT" or "DELETE" request using JSONHTTPClient? 有没有办法使用JSONHTTPClient发出“ PUT”或“ DELETE”请求?

Looking at the documentation here 在这里查看文档

http://jsonmodel.com/docs/Classes/JSONHTTPClient.html#//api/name/requestHeaders 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. 可以是另一个框架或NSURLSessions,也可以在JSONHTTPClient类上编写您自己的类别。

Anyway I came with the most stupid solution by adding new methods to JSONHTTPClient. 无论如何,我通过向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" 我还添加了一个新的const NSString以支持“ 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 . 我还在GitHub上创建了一个问题。 Hope to get response from there. 希望能从那里得到回应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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