0

所以我对此比较陌生,我遇到了一个让我很困惑的问题。SimpleUPC 提供了一个非常简单的 API,但它们具有以下 JSON 请求格式:

{
"auth":"Your API Key",
"method":"MethodName",
"params": {
    "paramName":"paramValue",
    "paramName2":"paramValue2",
},
"returnFormat":"optional"
}

我还下载了他们的 Ruby 示例,我已经验证它可以从命令行运行。

# Sample API calls using JSON...
host = "api.simpleupc.com"
path = "/v1.php"

# An example query for FetchProductByUPC method
request = {
    "auth" => 'Your-API-Key',
    "method" => 'FetchProductByUPC',
    "params" => {"upc" => '041383096013'}
}

json = request.to_json()


response = Net::HTTP.start(host) { |http|
    http.post(path, json, initheader = {'Content-Type' =>'text/json'})
}

output = response.body

puts output

好的,到目前为止一切都很好。但这是我的代码,它试图做同样的事情,但我收到错误,抱怨缺少参数。

NSDictionary *requestDict = @{@"auth": kSimpleAPIKey, @"method": @"FetchProductByUPC", @"params":@{@"upc": code}};

[[SimpleUPCClient sharedClient] getPath:@""
                             parameters:requestDict
                                success:^(AFHTTPRequestOperation *operation, id responseObject) {

                                    NSLog(@"Response: %@", responseObject);

                                }
                                failure:^(AFHTTPRequestOperation *operation, NSError *error) {

                                    [TMReportingHandler handleError:error fatal:NO];

                                }];

我知道这一定很简单,我做错了,但我无法弄清楚。任何人?

4

1 回答 1

0

哇,我知道这很简单。原来它期待的是 POST 而不是 GET ......我切换了它并且它立即工作。

于 2013-03-11T05:11:52.477 回答