发送到 http post 和设置实体时 IOS Json 错误

标签 ios json post

您好,我正在向服务器发送一个 http post 请求,我所指的 java 代码有一个方法作为 post 请求中的设置实体。我怎样才能在 iOS 中实现这一目标。

我目前正在将要发送的 json 数据放入正文中,但收到错误

Error 500 Cannot read request parameters due Invalid parameter, expected to be a pair

但是

{
  "chatMessage" : {
    "reportRequest" : "Sent",
    "text" : "wdfsds acd"
}

我的代码是

NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:[var.projectUsername base64EncodedString] forHTTPHeaderField:@"Authorization"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setHTTPShouldHandleCookies:YES];
   NSMutableDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:messageText.text,@"text",@"Sent",@"reportRequest",nil];
NSDictionary *dictionary1 = [NSDictionary dictionaryWithObjectsAndKeys:dictionary,@"chatMessage", nil];
     NSError *error = nil;
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:dictionary1  options:NSJSONWritingPrettyPrinted error:&error];



[request setHTTPBody:jsonData];

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection start];

最佳答案

在您的代码中添加:-
[request setValue:@"application/json"for HTTPHeaderField:@"Content-Type"];

关于发送到 http post 和设置实体时 IOS Json 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20758270/

相关文章:

ios - 尝试从 unwind segue 添加新的 webView 时如何清除 WKWebView?

javascript - 将表单值发布到 html

json - 有没有办法根据查询参数在响应中包含多个 json 文件?

PHP 对象到 JSON 格式

json - ExtJS 5.1 不会向我的服务器发送嵌套的 JSON 对象

objective-c - Restkit Post总是会出现故障

ios - 在 Google Maps API 上批量处理多个反向地理编码请求

ios - 类型名不允许指定存储类?

ios - 如何将参数传递给委托(delegate)回调方法?

arrays - 使用下划线js的嵌套类别搜索