【问题标题】:Unable to post large data in the backend using Rest API无法使用 Rest API 在后端发布大数据
【发布时间】:2016-04-15 13:00:24
【问题描述】:

我正在尝试将大数据存储在后端数据源中,但由于未知原因,REST API 在存储时将原始数据修剪成更小的块。

我提出请求的 Objective-C 函数是,

-(NSString *)getPostAPIResponseWithDestinationString:(NSString *)destinationString AndWithRequestString:(NSString *)requestString
{
    if([self isConnectedToInternet])
    {
        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", degsChickenBackendURL, destinationString]]];
        //NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", degsChickenBackendURL, destinationString]]];

        [request setHTTPMethod:@"POST"];
        NSString *postRequestString = [NSString stringWithFormat:@"%@", requestString];
        [request setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[postRequestString length]] forHTTPHeaderField:@"Content-length"];

        [request setHTTPBody:[postRequestString dataUsingEncoding:NSUTF8StringEncoding]];
        NSError *myError = nil;
        NSURLResponse *response;

        NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&myError];
        @try
        {
            return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        }
        @catch (NSException *exception)
        {
            //return @"Error";
        }
    }
    else
    {
        return @"Error";
    }
}

JSON字符串形式的原始数据是,

  {
  "OrderQuantity": "1",
  "OrderFoodDetails": {
    "FoodName": "¼ Chicken Leg and Thigh",
    "FoodPrice": "7.49",
    "FoodDescription": "Includes two fresh bread rolls with butter."
  },
  "OrderAddOns": [
    {
      "CategorySelectionLimit": "1",
      "CategoryCurrentValue": "Peri Peri Spice Level",
      "CategoryHeader": "Peri Peri Spice Level",
      "CategoryPrice": "0.000000",
      "CategoryItems": [
        {
          "ItemPrice": "0.000000",
          "ItemHeader": "Garlic - Simplicity",
          "ItemSelectionState": "0"
        },
        {
          "ItemPrice": "0.000000",
          "ItemHeader": "Lemon & Herb - Simplicity",
          "ItemSelectionState": "0"
        },
        {
          "ItemPrice": "0.000000",
          "ItemHeader": "Mild Peri Peri - Try it",
          "ItemSelectionState": "0"
        },
        {
          "ItemPrice": "0.000000",
          "ItemHeader": "Hot Peri Peri - Go for it",
          "ItemSelectionState": "1"
        },
        {
          "ItemPrice": "0.000000",
          "ItemHeader": "Extra Hot Peri Peri - Dare it",
          "ItemSelectionState": "0"
        }
      ],
      "CategorySelectionState": "0"
    },
    {
      "CategorySelectionLimit": "0",
      "CategoryCurrentValue": "Drinks",
      "CategoryHeader": "Drinks",
      "CategoryPrice": "0.000000",
      "CategoryItems": [
        {
          "ItemPrice": "1.490000",
          "ItemHeader": "Bottled Water",
          "ItemSelectionState": "0"
        },
        {
          "ItemPrice": "2.590000",
          "ItemHeader": "Coca Cola Freestyle Large",
          "ItemSelectionState": "0"
        },
        {
          "ItemPrice": "2.290000",
          "ItemHeader": "Coca Cola Freestyle Regular",
          "ItemSelectionState": "0"
        }
      ],
      "CategorySelectionState": "0"
    },
    {
      "CategorySelectionLimit": "10",
      "CategoryCurrentValue": "Add Ons",
      "CategoryHeader": "Add Ons",
      "CategoryPrice": "0.000000",
      "CategoryItems": [
        {
          "ItemPrice": "2.990000",
          "ItemHeader": "Deg’s Signature Wurlys",
          "ItemSelectionState": "0"
        },
        {
          "ItemPrice": "4.490000",
          "ItemHeader": "Deg’s Signature Wurlys with Perinaise",
          "ItemSelectionState": "0"
        },
        {
          "ItemPrice": "2.490000",
          "ItemHeader": "Healthy Fries",
          "ItemSelectionState": "0"
        },
        {
          "ItemPrice": "3.990000",
          "ItemHeader": "Healthy Fries with Perinais",
          "ItemSelectionState": "0"
        },
        {
          "ItemPrice": "2.490000",
          "ItemHeader": "Mashed Potatoes",
          "ItemSelectionState": "0"
        },
        {
          "ItemPrice": "2.490000",
          "ItemHeader": "Baked Potatoes",
          "ItemSelectionState": "0"
        },
        {
          "ItemPrice": "2.490000",
          "ItemHeader": "Coleslaw",
          "ItemSelectionState": "0"
        },
        {
          "ItemPrice": "2.490000",
          "ItemHeader": "Corn",
          "ItemSelectionState": "0"
        },
        {
          "ItemPrice": "2.490000",
          "ItemHeader": "Brown Rice",
          "ItemSelectionState": "0"
        },
        {
          "ItemPrice": "2.490000",
          "ItemHeader": "Perinaise",
          "ItemSelectionState": "0"
        },
        {
          "ItemPrice": "2.490000",
          "ItemHeader": "2 Freshly baked Dinner Rolls with Butter",
          "ItemSelectionState": "0"
        }
      ],
      "CategorySelectionState": "0"
    }
  ],
  "OrderTotalPrice": "7.490000",
  "OrderSinglePrice": "7.490000",
  "OrderUserName": "kl's Order"
}

修剪后的块是,

"{\\\"OrderQuantity\\\":\\\"1\\\",\\\"OrderFoodDetails\\\":{\\\"FoodName\\\":\\\"\u00bc Chicken Leg and Thigh\\\",\\\"FoodPrice\\\":\\\"7.49\\\",\\\"FoodDescription\\\":\\\"Includes two fresh bread rolls with butter.\\\"},\\\"OrderAddOns\\\":[{\\\"CategorySelectionLimit\\\":\\\"1\\\",\\\"CategoryCurrentValue\\\":\\\"Peri Peri Spice Level\\\",\\\"CategoryHeader\\\":\\\"Peri Peri Spice Level\\\",\\\"CategoryPrice\\\":\\\"0.000000\\\",\\\"CategoryItems\\\":[{\\\"ItemPrice\\\":\\\"0.000000\\\",\\\"ItemHeader\\\":\\\"Garlic - Simplicity\\\",\\\"ItemSelectionState\\\":\\\"0\\\"},{\\\"ItemPrice\\\":\\\"0.000000\\\",\\\"ItemHeader\\\":\\\"Lemon "

我在发出 API 请求时是否犯了错误或遗漏了什么?我很抱歉,但上面的请求代码总是有效的。

【问题讨论】:

    标签: ios objective-c json api xcode7


    【解决方案1】:

    除了设置“内容长度”之外,您还需要设置内容类型。在您的代码中尝试以下操作?

    NSString *postLength = [NSString stringWithFormat:@"%d", [postRequestString length]];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/json" forHTTPHeaderField:@"content-type"];
    [request setValue:@"utf-8" forHTTPHeaderField:@"charset"];
    

    【讨论】:

      猜你喜欢
      • 2016-08-19
      • 1970-01-01
      • 2015-04-27
      • 1970-01-01
      • 1970-01-01
      • 2011-11-12
      • 1970-01-01
      • 2021-05-03
      • 2015-02-20
      相关资源
      最近更新 更多