在NSURLRequest中通过POST发送JSON [英] Sending a JSON via POST in NSURLRequest
问题描述
我在使用REST API向服务器发送JSON时遇到问题。
这是我使用的代码:
I have a problem with sending a JSON to a Server with REST API. This is the code i use:
NSString *jsonPostBody = [NSString stringWithFormat:@"'json' = '{\"user\":{\"username\":"
"\"%@\""
",\"password\":"
"\"%@\""
"}}'",
[username stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
[password stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSData *postData = [jsonPostBody dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSString *apiPathParams = [NSString stringWithFormat:@"%@",
@"getUser"
];
NSURL *url = [NSURL URLWithString:[[apiPath retain] stringByAppendingString:apiPathParams]];
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:180.0];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:postData];
NSString* postDataLengthString = [[NSString alloc] initWithFormat:@"%d", [postData length]];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:postDataLengthString forHTTPHeaderField:@"Content-Length"];
[self internalRequest:request];
这就是API的样子,如果是html格式:
This is what the API would look like, if it was a html form:
<form method="post" action="someAPI-URL">
<input name="json" value="{"user":...}" />
</form>
这是我将POST数据添加到请求时的样子:
This is what my POST Data looks like when i add it to the request:
json={"user":{"username":"someUser","password":"somePassword"}}
由于某种原因,我不知道POST数据没有到达服务器。
我对dataString的格式化有什么问题吗?
我必须如何格式化我的dataString以便它匹配字符串如上所示的表格将传送到服务器?
For a reason I don't know the POST Data does not arrive at the server. Have i done something wrong with the formatting of the dataString? How exactly must i format my dataString so that it matches the String a form as shown above would deliver to the server?
任何帮助都将受到高度赞赏。
Any help would be highly appreciated.
PS我宁愿不使用ASIHttpRequest,因为我从其他人那里接管了该项目,其他所有请求都正常工作,除了这个post-request。
所以将整个批量更改为其他连接框架将非常耗时。
P.S. I would rather not use ASIHttpRequest, since i took over the project from somebody else an every other request works fine, except this post-request. So changing this whole bulk to an other connection framework would be very time consuming.
这是internalRequest方法的源代码
Here is the internalRequest Method's sourcecode
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:req delegate:self];
NSMutableDictionary *di = [NSMutableDictionary dictionaryWithObject:[[NSMutableData alloc] init] forKey:@"receivedData"];
[di setObject:[NSString stringWithFormat:@"%i",identifier] forKey:@"identifier"];
if (delegate == nil)
{
delegate = self;
}
[di setObject:delegate forKey:@"delegate"];
CFDictionaryAddValue(connectionToInfoMapping, connection, di)
推荐答案
因为您尝试提交HTTP POST标头,例如
since you try to submit a HTTP POST header like
json = {user:{username:% @,密码:%@}}
,
此示例完全有资格最终混淆。
this example is fully qualified to end up in confusion.
这是整个身体和应用程序的
代表价值。 application / x-www-form-urlencoded
的混合体/ json
It's a mixture of application/x-www-form-urlencoded
for the whole body and application/json
for the value.
也许可以解决这个问题:
您可能需要调整该HTTP标头:
You'll probably need to adjust that HTTP header:
[request setValue:@"application/x-www-form-urlencoded"
forHTTPHeaderField:@"Content-Type"];
由于HTTP主体的JSON部分(值)的编码:
due to an encoding of the JSON part (value) of the HTTP body:
[request setHTTPBody:[[jsonPostBody stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]
dataUsingEncoding:NSUTF8StringEncoding
allowLossyConversion:YES]];
其中 stringByAddingPercentEscapesUsingEncoding
是客观的c jabberwocky PHP等价物 urlencode
。
where stringByAddingPercentEscapesUsingEncoding
is objective c jabberwocky for the PHP equivalent urlencode
.
HTH
这篇关于在NSURLRequest中通过POST发送JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!