无法使用Rest API在后端发布大数据

问题描述 投票:0回答:1

我试图在后端数据源中存储大数据,但由于未知原因,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 json objective-c xcode7
1个回答
0
投票

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

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"];
© www.soinside.com 2019 - 2024. All rights reserved.