[英]HTTP PUT or DELETE request with JSONHTTPClient in iOS and OS-X
我剛剛開始使用JSONModel 。 JSONHTTPClient可以處理支持“ GET”和“ POST”方法的異步網絡請求。 有沒有辦法使用JSONHTTPClient發出“ PUT”或“ DELETE”請求?
在這里查看文檔
http://jsonmodel.com/docs/Classes/JSONHTTPClient.html#//api/name/requestHeaders
看來您無法做到。 您將必須使用另一種方法來執行此操作。 可以是另一個框架或NSURLSessions,也可以在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;
我還添加了一個新的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);
}];
}
我還在GitHub上創建了一個問題。 希望能從那里得到回應。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.