【问题标题】:How to delete an recipe in form of JSON from a server?如何从服务器中删除 JSON 格式的配方?
【发布时间】:2015-10-23 08:13:15
【问题描述】:

我编写了一个代码,从服务器获取 JSON 格式的食谱并将其呈现到 UItable 中。

NSURL *url = [NSURL URLWithString:@"http://domain.com/recipes"];
    [config setHTTPAdditionalHeaders:@{@"Authorization":@" Token token=\"3f71235466468b7f7\""}];

    NSURLSession *session = [NSURLSession sessionWithConfiguration:config delegate:nil delegateQueue:[NSOperationQueue mainQueue]];

    [[session dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        recipes= [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
       [self.tableView reloadData];

    }
      ] resume];

此服务器是用 Ruby 编写的。现在我想写一个删除食谱的请求。这是我的服务器的说明。它说我应该使用以下 curl 删除食谱:

删除食谱 DELETE:/recipes/:id 在 Curl 中会是这样的:

curl -H 'Authorization: Token token="0b774d575632b"' -X DELETE http://domain.com/recipes/22

有人知道我该如何实现删除方法吗?

【问题讨论】:

  • 这只是一个 http 请求...像处理其他任何请求一样处理它。
  • @MarcB thx,我应该像发布某事时那样执行请求吗?

标签: ios curl nsurlsession http-delete nsurlsessiontask


【解决方案1】:

您应该使用dataTaskWithRequest:,而不是只允许 GET 请求的dataTaskWithURL:

使用适当的 URL 创建一个 NSMutableURLRequest,并将其 HTTPMethod 属性设置为 @"DELETE"。然后,您可以在 NSURLSession 中使用该请求。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-12
    • 2012-10-03
    • 2018-11-06
    • 1970-01-01
    相关资源
    最近更新 更多