gpt4 book ai didi

objective-c - Node.js bodyparser 无法准备好 AFNetworking POST 数组

转载 作者:塔克拉玛干 更新时间:2023-11-01 19:08:35 25 4
gpt4 key购买 nike

我正在尝试将包含数组的 JSON 消息发布到我的服务器。但是服务器无法识别数组中的元素。检查问题后,我发现了我认为的问题所在。

AF网络代码:

AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:kServiceBaseUrl]];
manager.responseSerializer = [AFJSONResponseSerializer serializer];

[manager PUT:@"/api/createRubaway"
parameters:myDictionary
success:^(NSURLSessionDataTask *task, NSDictionary *responseObject)
{/* stuff */}
failure:^(NSURLSessionDataTask *task, NSError *error)
{/* stuff */}
];

LLDB 打印出“myDictionary”:

{
description = description;
title = title;
transitions = (
{
idx = 0;
resource = {
data = testdata;
name = transition;
type = image;
};
threshold = "0.75";
type = rubaway;
},
{
idx = 1;
resource = {
data = testdata;
name = transition;
type = image;
};
threshold = "0.75";
type = rubaway;
}
);
}

在 AFNetworking 序列化请求之后。如果我从 NSMutableRequest 打印出 HTTPBody,这就是它发送的内容(这是发送到我的服务器的内容):

description=description&title=title&transitions[][idx]=0&
transitions[][resource][data]=testdata&
transitions[][resource][name]=transition&
transitions[][resource][type]=image&
transitions[][threshold]=0.75&
transitions[][type]=rubaway&transitions[][idx]=1&
transitions[][resource][data]=testdata&
transitions[][resource][name]=transition&
transitions[][resource][type]=image&transitions[][threshold]=0.75&
transitions[][type]=rubaway

以上格式是否正确。作为我的服务器(Node.js - BodyParser),我相信它会以这种格式出现 https://stackoverflow.com/a/18402556/623750

有没有办法改变 JSON 序列化器的格式?或者有没有办法配置 BodyParser 以接受上述格式?

谢谢,瑞安

最佳答案

您需要做的就是将 requestSerializer 设置为 AFJSONRequestSerializer,这将解决问题。

 [manager setRequestSerializer:[AFJSONRequestSerializer serializer]];
[manager setResponseSerializer:[AFJSONResponseSerializer serializer]];

关于objective-c - Node.js bodyparser 无法准备好 AFNetworking POST 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26177646/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com