无法使用UNIRest在iOS Objective C中发出正确的帖子请求

时间:2016-09-15 04:03:28

标签: ios objective-c iphone delegates http-post

我正在尝试使用按钮从IOS应用程序发出特定URL的发布请求,并使用第二个按钮检索这些发布的参数。

这是我目前正在做的事情:

- (IBAction)firstButton:(id)sender {
    NSString *id = @"12345";
    NSDictionary* headers = @{@"accept": @"application/json"};
    session = [NSString stringWithFormat:@"http://xyz.io/sessions/%@/", id];
    NSDictionary* parameters = @{@"session_url": session};
    [[UNIRest post:^(UNISimpleRequest *request) {
        [request setUrl:[NSString stringWithFormat:@"%@devices/%@", BASE_URL, @"914-11e6-b775-02"]];
        [request setHeaders:headers];
        [request setParameters:parameters];
    }] asJsonAsync:^(UNIHTTPJsonResponse* response, NSError *error) {
        if (error) {
            NSLog(@"post request submit error = %@", error);
            return;
        }
        else
        {
            NSLog(@"post request success");
        }
    }
     ];}

和获取参数的第二个按钮如下所示:

    - (IBAction)secondButton:(id)sender{
       NSString *deviceId = @"914-11e6-b775-02";
        NSDictionary* headers = @{@"accept": @"application/json"};

        [[UNIRest get:^(UNISimpleRequest *request) {
            [request setUrl:[NSString stringWithFormat:@"%@devices/%@", BASE_URL, deviceId]];
            [request setHeaders:headers];
        }] asJsonAsync:^(UNIHTTPJsonResponse* response, NSError *error) {
            // This is the asyncronous callback block
            UNIJsonNode *body = response.body;

            if (error) {
                NSLog(@"error = %@", error);
                return;
            }
            else
            {
                NSLog(@"session = %@", body.JSONObject[@"session_url"]);
            }
         }
      ];}

即使我从firstButton发布了“ session_url ”对象,当我试图从secondButton获取它时,它总是返回(null)

我不确定为什么会这样,任何帮助都会受到赞赏。

P.S。在firstButton中,我在控制台日志中获得了“post request success”

1 个答案:

答案 0 :(得分:0)

没关系,我找到了解决方案这是我正在做的一个非常小的错误。

我更改了"帖子"请求" put"在firstButton,它对我有用。

任何人都在寻找帖子和看跌请求之间的区别: https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html