【问题标题】:Couldn't make a proper post request in iOS Objective C using UNIRest无法使用 UNIRest 在 iOS 目标 C 中发出正确的发布请求
【发布时间】:2016-09-15 04:03:27
【问题描述】:

我正在尝试使用按钮从 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)

我不知道为什么会这样,任何帮助将不胜感激。

附:在 firstButton 中,我在控制台日志中收到“发布请求成功”

【问题讨论】:

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


    【解决方案1】:

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

    我在 firstButton 中将“发布”请求更改为“放置”,它对我有用。

    任何寻找 post 和 put 请求之间区别的人: https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html

    【讨论】:

      猜你喜欢
      • 2020-12-13
      • 2019-09-17
      • 2022-11-28
      • 1970-01-01
      • 1970-01-01
      • 2012-12-11
      • 1970-01-01
      • 2018-10-31
      • 2015-10-12
      相关资源
      最近更新 更多