如何设置“PUT”的数据?请求AFNetworking? [英] How do I set the data for a "PUT" request with AFNetworking?

查看:215
本文介绍了如何设置“PUT”的数据?请求AFNetworking?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始使用AFNetworking,它可以很好地进行简单的GET请求。但是现在我正在尝试做一个POST - 请求。我使用下面的代码来执行GET请求。查看 AFHTTPClient puthPath 时,无法设置要用于正文的数据。我的猜测是,有另一种解决方法。我一直在寻找 AFHTTPOperation 来解决这个问题。但是,我没有让这个工作。问题是我不知道如何在基本身份验证中使用它。

I have started to use AFNetworking and it works well when it gets to making simple "GET"-request. However now I am trying to do a "POST"-request. I use the code below to do the "GET" request. When looking at the puthPath of AFHTTPClient there is no way to set the data to use for the body. My guess is that there is another way of fixing this. I have been looking at the AFHTTPOperation as a way of fixing this. However I am not getting this to work. The problem is that I do not know how to use it with Basic Authentication.

有人可以给我一些关于如何使用AFNetworking进行简单POST请求的提示?

Could somebody give me a hint of how to do a simple "POST"-request with AFNetworking?

AFHTTPClient* client = [AFHTTPClient clientWithBaseURL:ServerURL];
[client setAuthorizationHeaderWithUsername:self.username 
                                  password:self.password];

NSString* resourcePath = [NSString stringWithFormat:@"/some/resource/%@", 
                          endPath];

[client getPath:resourcePath 
     parameters:nil 
        success:^(AFHTTPRequestOperation *operation, id responseObject) {
            // Success code omitted
        } 
        failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            // Some error handling code omitted
        }
 ];


推荐答案

我没有找到任何简单的方法来做到这一点。但我按照建议做了并创建了我自己的 AFHTTPClient 子类。在子类中,我实现了以下方法。这使得可以执行POST请求和&使用我自己的数据进行PUT请求。

I did not find any easy way to do this. But I did as recommended and created my own sub-class of AFHTTPClient. In the subclass I implemented the methods below. This makes it possible to perform both POST-request & PUT-requests with my own data.

- (void)postPath:(NSString *)path 
  parameters:(NSDictionary *)parameters 
        data:(NSData*)data
     success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
     failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;
{
    NSURLRequest *request = [self requestWithMethod:@"POST" path:path     parameters:parameters data:data];
    AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
    [self enqueueHTTPRequestOperation:operation];
}

- (void)putPath:(NSString *)path 
     parameters:(NSDictionary *)parameters 
           data:(NSData*)data
        success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
        failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;
{
    NSURLRequest *request = [self requestWithMethod:@"PUT" path:path parameters:parameters data:data];
    AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
[self enqueueHTTPRequestOperation:operation];
}

-(NSMutableURLRequest*)requestWithMethod:(NSString *)method 
                                    path:(NSString *)path 
                              parameters:(NSDictionary *)parameters 
                                 data:(NSData*)data;
{
    NSMutableURLRequest* request = [super requestWithMethod:method 
                                                      path:path 
                                                parameters:parameters];

    [request setHTTPBody:data];

    return request;
}

这篇关于如何设置“PUT”的数据?请求AFNetworking?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆